Skip to main content

Cloud

Delete All Old Versions from SharePoint 2010 Document Libraries

How many versions of a typical document do you need to keep? 5? 10? 100?
 
SharePoint content databases often get cluttered with redundant versions of the same documents. In some cases, I’ve seen presentation libraries with less than 10 GB of active content, but hundreds of GBs of content due to gradual changes.
 
If you’re ready to clear out old versions, SharePoint 2010’s Management Shell makes it easy. The script below will iterate through all lists and update them to keep only the last 2 copies. It will also loop through and delete unneeded versions.
 
GetSPWebApplication|GetSPSiteLimit All|GetSPWebLimit All|ForEachObject{ForEach($listin$_.Lists){If($list.EnableVersioningeq $true){$list.MajorVersionLimit=2;$list.Update();ForEach($itemin$list.Items){$item.URL;$item.SystemUpdate()}}}}
 
You can see that it…
  1. Loops through all web applications
  2. Loops through their site collections
  3. Loops through their webs
  4. Loops through their lists
  5. If versioning’s enabled, it sets the major version limit to 2
  6. In order to remove old versions, it needs to loop through each item and perform a system update

The script can be easily amended to deal with only specific site collections / libraries. To keep a different number of versions, modify the MajorVersionLimit variable above.

Thoughts on “Delete All Old Versions from SharePoint 2010 Document Libraries”

  1. Hi,
    how do I change the code for a specific Web App, Site Collection, library? Can you give an example?
    thanks,
    Daverro

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bert Johnson

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram