Clearing out all SharePoint document versions for a set of documents

Wiping out all SharePoint document versions for a set of documents

Sometimes users create many versions of a document inadvertently. An example is a PDF, where properties may have been edited frequently. In SP2013 there is Shredded Storage, which handles storage of deltas (differential save). in SharePoint 2010 versions can result in a lot of wasted disk storage. Let’s clean it up!

$web=Get-SPWeb "ht tp://SharePoint"
for ($i=0;$i -lt $web.Lists.Count;$i++)
{ 
$JPLib=$web.Lists[$i];
$A_Lib_Count++;
$SkipLib=$true; #true
if ( ($JPlib.BaseType -ne "DocumentLibrary") -or ($JPlib.hidden) )
{
# forget the rest and return to top
Write-Host -foregroundcolor blue "fast test skipping Library: $($JPlib)";   
Add-Content $mylogfile "Skipping Library: $($JPlib.title)`n";   
}
elseif ($JPLib.Title -Match "SitesAssets|Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide")
{
# forget the rest and return to top
Write-Host -foregroundcolor blue "fast test skipping Library because it mentions $Matches: $($JPlib)";   
Add-Content $mylogfile "Skipping Library: $($JPlib.title)`n";   
}
elseif ($JPLib.BaseTemplate -ne "DocumentLibrary")   #alternatively, only skip if -eq XMLForm
{
# forget the rest and return to top
Write-Host -foregroundcolor blue "fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)";   
Add-Content $mylogfile "fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)`n";   
}
elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq "AllSlides"))
{
# forget any library with thumbnails, these are not normal doclibs, and return to top
Write-Host -foregroundcolor blue "fast test skipping Library because it has Thumbnails/Slides $($JPlib)";   
Add-Content $mylogfile "fast test skipping Library because it has Thumbnails/Slides: $($JPlib)`n";   
}
else
{  $SkipLib=$false;	}
if (!$SkipLib)
{
write-Host -foregroundcolor darkgreen "Processing Library to Merge: $($JPlib)";   
Add-Content $mylogfile "Processing to merge: $($JPlib)`n"; 
$JPItems = $JPLib.items;
$JPCount = $JPitems.count;
for ($itemIndex=0; $itemIndex -lt $JPCount; $itemIndex++)
{
$JPItem=$JPItems[$itemIndex];
$FName=$JPItem.file.name;
if ($FName -match ".pdf") #you choose the criteria, here I match on pdfs
{
$JPFile=$JPItem.file;
Write-Host "Deleting $($JPFile.Versions.Count), size of each: $($JPFile.properties.vti_filesize),$($JPitem.url)"
$JPfile.Versions.DeleteAll()
}
continue; # don't mess with timestamp/author just yet
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
[System.DateTime]$date = $SourceItem["Modified"]
$TargetItem["Modified"]=$date;
try {
$TargetItem["Editor"] = $SourceItem["Editor"]
}
catch
{
write-host -foregroundcolor red "Error could not assign editor of $($targetItem.url)"
}
$TargetItem.update()   
try
{  # two deletes required
$TargetItem.Versions[1].delete()
$TargetItem.Versions[1].delete()
}
catch
{
write-host -foregroundcolor red "Warning: could not delete old version of $($targetItem.url)"
}
})
}
}
}  
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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