0
votes

I currently have a SharePoint 2013 environment which was upgraded from SharePoint 2010 a year or so ago.. the versioning on some document libraries where set to unlimited versions, and was consuming a lot of additional space (on some document libraries around 30GB). I have decided to remove the versioning to back to 5 (the 5 latest versions).

The below script has had some success - and when running on multiple document libraries it has remove versioning, but on files within the same document library the versioning hasn't been removed.

Example: Before running the script file 1 = had 100 versions file 2 = had 100 versions

After running the script file 1 = has 5 versions file 2 = has 100 versions

    $SPweb = Get-SPweb "xxxxx"      ##EDIT AND PUT THE WEB ADDRESS HERE##
    $versionsToKeep =5; ##NUMBER OF VERSIONS TO KEEP##
    $SPlist = $SPweb.Lists["Document Library"]      ##THE DOC LIBRARY##

    foreach ($SPitem in $SPlist.Items)
        {
    $currentVersionsCount= $SPItem.Versions.count

        if($currentVersionsCount -gt $versionstoKeep)
        {
    for($i=$currentVersionsCount-1; $i -ge $versionstoKeep; $i--)
        {
    $SPItem.versions[$i].delete()
        }
        }
        }

I'm unsure why this would be - and if anyone else has had these issues previously?

1
Can't you just set the limitation on the library to 5 versions and then loop through the library and update each item with a powershell script? Then all versions should be cleared, or? - dns_nx
@dns_nx that sounds like it would work, any ideas on a PowerShell script to loop through a document library? - MattG
Yes, please see my answer. - dns_nx

1 Answers

0
votes

Ok, so here is the solution in my opinion. Set the document library version limit to 5 and then loop through the library and update each item with a powershell script.

$web = Get-spweb http://yourweb/sites/path
$list = $web.Lists["Document Library"]
ForEach( $file in $list.Items ) { 
    $file.Update() 
}