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.
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.
In case, You want to enable/disable the version for a particular list/library: say tasks list
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!
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.
Alternatively, You can perform an empty update in list items to apply new versioning limit and cleanup older versions.
When you have large number of versions in SharePoint list, iterating through versions may result in performance hits. So, the alternate method goes below:
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 Version
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.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # 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()
}
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!
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $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() } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
| $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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #Variables for Processing$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 Version
Read more: http://www.sharepointdiary.com/2011/01/limit-versioning-and-delete-old-versions-in-sharepoint.html#ixzz3Ucl9MAZ8


No comments:
Post a Comment