Flushing versions from SharePoint document libraries

Say a SharePoint document library has documents with over 10 versions each.

You set the version limit to 5 and now what to flush the versions past 5.

Setting the new limit doesn't flush the old versions.  Only making a new version will.  But that will disturb the metadata unnecessarily.

Is there a way to flush the extraneous versions?  Stsadm, webservice, or any other method?  I'm able to identify the guid for the doc/version.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could write a utility that goes via the object model and executes a SPListItem.SystemUpdate() method against each document.


This will perform an update on the list item, purge the versions, but keep the metadata, modified by, etc intact.

Test this process in a sandbox environment and insure you get your desired results.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OCSDTechAuthor Commented:
I used some PowerShell from quihong and the SystemUpdate() method fro liebrand to come up with the following powershell script.

Set the $WebAppURL to your web app.

The script looks for items with greater than 5 versions.  Each item will be "touched" without updating the metadata to trigger the extraneous versions to be removed.

Removing the # and editing the $web.URL limits the script to a site.

Removing the ## activates the $item.SystemUpdate ().

Test before using.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
# Create the stopwatch
[System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics.Stopwatch") > $null
$sw = New-Object System.Diagnostics.StopWatch

$startime = Get-Date

$WebAppURL =  "http://sharepoint"
$theSite = New-Object Microsoft.SharePoint.SPSite($webAppURL)
$webApp = $theSite.WebApplication
$allSiteCollections = $webApp.Sites

foreach($site in $allSiteCollections)
	foreach($web in $site.AllWebs)
		foreach($list in $web.Lists)
			if($list.EnableVersioning -and $list.Title -ne "Master Page Gallery" -and $list.Title -ne "Workflows" -and $list.GetType().Name -eq"SPDocumentLibrary")
#            if ($web.URL -eq"http://sharepoint/subsite" )# -and $list.Title -eq "My Library")
#                {
                foreach($item in $list.items)
                     if ($item.Versions.Count -gt 5)
                     Write-Host $item.Name   $item.Versions.Count
##                     $item.SystemUpdate()
#                }

Open in new window

OCSDTechAuthor Commented:
Added powershell script to complete the solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.