Within our SDLC farms (Dec, QA, UAT) we have limited amounts of resources.  As these are testing and integration environments there is often no reason to keep more than one or two copies of versions in the document libraries, especially as they are refreshed from production content databases on a regular basis.

Often there are 10-20GB of actual latest content, and 50GB of versions.  In some cases we can remove them from production, in others we only need to clear out all but the latest, as with the case of our SDLC farms.  Here is the PowerShell script that can be used to remove old versions.  You can set the limit lower for SDLC environments and higher if you want to trim production.

Get-SPWebApplication | Get-SPSite -Limit All | Get-SPWeb -Limit All | ForEach-Object { ForEach($list in $_.Lists) { If($list.EnableVersioning -eq $true) { $list.MajorVersionLimit = 1; $list.Update(); ForEach($item in $list.Items) { $item.URL; $item.SystemUpdate() } } } }

 

You can see that it…

  1. Gets all web applications in the farm
  2. All site collections
  3. All webs
  4. Loops through all lists  ( you can filter for Title if you wish: if($_.Title -eq "Shared Documents") in order ot only handle the default Shared Documents folder )
  5. If versioning’s enabled, it sets the major version limit to 1, you can set it higher for production environments
  6. In order to remove old versions, it needs to loop through each item and perform a system update

To keep a different number of versions, modify the MajorVersionLimit variable above.