Saturday, January 15, 2011

Limit Versioning and Delete Old Versions in SharePoint

In one of my SharePoint Implementation, Defined the governance policy for versioning as: "Only 5 Major and 5 Minor versions should be kept.". So placed that in End user self service Portal site. But who cares? users setup up their document libraries/Lists to unlimited number of versions, and asking for Increasing the Site quota quite often.

On analyzing the site space figured out: For a 1GB site, versions are occupying nearly 300MB!!! So, wanted to clean-up the versions except last 5 minor/Last 5 Major.

Oh, Yeah, to set versioning limit in SharePoint, Just get into the List/Library settings > Versioning settings then setup the versioning limits.
Limit Versioning and Delete Old Versions in SharePoint

Quite simple, Isn't it? Ah, but user has 100+ libraries where unlimited number of versions enabled! OMG!!!

Limit number of versions in SharePoint with PowerShell
PowerShell comes to rescue again! Here is the code to delete the older versions, except last 5 major and minor.

$SPsiteCollection = Get-SPSite "http://SharePointSite.com"  
    # Loop through all Webs in the Site Collection  
    foreach($SPweb in $SPsiteCollection.AllWebs)   
     {  
        # loop through all lists in web
   foreach ($SPlist in $SPweb.Lists)
       {  
  if($SPlist.EnableVersioning=$true)
          {
   write-host "Setting versioning Limit for :" $SPlist.title 
                 $SPlist.MajorVersionLimit = 5;
   # To Disable Versioning use: $SPlist.EnableVersioning=$false
   # To Enable Minorversion: $SPlist.EnableMinorVersions = $true;
   # Minor version Limit:  $SPlist.MajorWithMinorVersionsLimit = 5;

                 $SPlist.Update();
              }
              }  
     } 


In case, You want to enable/disable the version for a particular list/library: say tasks list

 $ListTitle="Tasks"
 if($SPlist.Title -eq $ListTitle)
  {
   # To Enable Versioning
   $SPlist.EnableVersioning = $true
   $SPlist.Update()
  }

Ok, What about cleaning the Old versions?

I have set-upped versioning limits, Will it delete the older version beyond my limit? unfortunately "NO"

Say for e.g. I've a Document in Document Library with unlimited number of versions turned ON, Where the document has 20 versions. So, If I Set the versioning limit to 5, will it delete the Older 15 versions and Keep only the last 5? NO NO NO, Until we create a new document or edit the existing document, those versioning limits will not be applicable. This new limit will not affect the existing old version. We need to Delete them explicitly.

Take a Look at this task list, where I've limited the versions NOW to 3, But it has existing older versions!
Limit Cleanup SharePoint Versioning

Programmatically delete old versions with PowerShell
So, To clean up the existing versions, Lets seek help from PowerShell again! Let the PowerShell delete older versions.
$SPweb = Get-SPWeb "http://SharePointSite.com"
$versionsToKeep =5;
$SPlist = $SPweb.Lists["Tasks"]
 foreach ($SPitem in $SPlist.Items)
 {
   $currentVersionsCount= $SPItem.Versions.count

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

Alternatively, You can perform an empty update in list items to apply new versioning limit and cleanup older versions.
$SPweb = Get-SPWeb "http://sp10test.brightpoint.com"
$versionsToKeep =3;
$SPlist = $SPweb.Lists["Tasks"]

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

   if($currentVersionsCount -gt $versionstoKeep)
     {
        $SPListItem.SystemUpdate()
     }
 }

When you have large number of versions in SharePoint list, iterating through versions may result in performance hits. So, the alternate method goes below:
#Variables for Processing
$WebURL = "http://sharepoint.crescent.com/operations/imphilippineswksp/QII/"
$ListName ="Project Cost"
$ItemID="512"

#Get the Web and List objects
$web = Get-SPWeb($WebURL)
$list = $web.Lists[$ListName]

#Set list version setting
$list.MajorVersionLimit = 10
$list.Update() 

#Get the List Item
$item = $list.GetItemById($itemId)
 
#Perform a empty update - without creating new version, so that versioning changes will take effect
$item.SystemUpdate($false) 

write-host "Total Number of versions Now:"$Item.Versions.count
 
#Dispose 
$web.dispose();

Update: I've created an utility to limit and cleanup old versions of documents. You can download it here:
SharePoint Versioning Manager - Control Versioning Settings & Clean Up Old Versions



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

5 comments :

  1. How will be the script for minor versions?

    ReplyDelete
    Replies
    1. You can set List.EnableMinorVersions and list.MajorWithMinorVersionsLimit properties.

      Delete
  2. Shouldn't the line:

    for($i=$currentVersionsCount-1; $i -gt $versionstoKeep; $i--)

    Instead be:

    for($i=$currentVersionsCount-$versionstoKeep; $i -gte 1; $i--)

    For instance, if there are 20 versions and you want to keep 5 this would start with version 15 and delete older versions (lower number versions) through version 1.

    It seems the line you used would delete all but the oldest 5. Am I reading it wrongly?

    ReplyDelete
    Replies
    1. Hi there,

      The above code aims to keep the latest versions and delete the old one. Versions are stored as First-in-first-out.

      Delete
  3. When I try to delete old versions, getting the below error. Any idea.

    You cannot call a method on a null-valued expression.
    At E:\test\Vers_del.ps1:53 char:53
    + $Item.Versions[770.0].delete <<<< ()
    + CategoryInfo : InvalidOperation: (delete:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...