{"id":1943,"date":"2013-05-04T06:04:10","date_gmt":"2013-05-04T06:04:10","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1943"},"modified":"2024-04-26T13:04:37","modified_gmt":"2024-04-26T13:04:37","slug":"clearing-out-all-sharepoint-document-versions-for-a-set-of-documents","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/clearing-out-all-sharepoint-document-versions-for-a-set-of-documents\/","title":{"rendered":"Clearing out all SharePoint document versions for a set of documents"},"content":{"rendered":"<h2>Wiping out all SharePoint document versions for a set of documents<\/h2>\n<p>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\u2019s clean it up!<\/p>\n<pre lang=\"php\">$web=Get-SPWeb \"ht tp:\/\/SharePoint\"\r\n \r\n   for ($i=0;$i -lt $web.Lists.Count;$i++)\r\n   { \r\n   $JPLib=$web.Lists[$i];\r\n   $A_Lib_Count++;\r\n   $SkipLib=$true; #true\r\n    \r\n   if ( ($JPlib.BaseType -ne \"DocumentLibrary\") -or ($JPlib.hidden) )\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor blue \"fast test skipping Library: $($JPlib)\";   \r\n      Add-Content $mylogfile \"Skipping Library: $($JPlib.title)`n\";   \r\n    }\r\n    elseif ($JPLib.Title -Match \"SitesAssets|Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide\")\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor blue \"fast test skipping Library because it mentions $Matches: $($JPlib)\";   \r\n      Add-Content $mylogfile \"Skipping Library: $($JPlib.title)`n\";   \r\n    }\r\n    elseif ($JPLib.BaseTemplate -ne \"DocumentLibrary\")   #alternatively, only skip if -eq XMLForm\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor blue \"fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)\";   \r\n      Add-Content $mylogfile \"fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)`n\";   \r\n    }\r\n    elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq \"AllSlides\"))\r\n    {\r\n      # forget any library with thumbnails, these are not normal doclibs, and return to top\r\n      Write-Host -foregroundcolor blue \"fast test skipping Library because it has Thumbnails\/Slides $($JPlib)\";   \r\n      Add-Content $mylogfile \"fast test skipping Library because it has Thumbnails\/Slides: $($JPlib)`n\";   \r\n    }\r\n    else\r\n    {  $SkipLib=$false; }\r\n      \r\n      \r\n    if (!$SkipLib)\r\n    {\r\n      write-Host -foregroundcolor darkgreen \"Processing Library to Merge: $($JPlib)\";   \r\n      Add-Content $mylogfile \"Processing to merge: $($JPlib)`n\"; \r\n        \r\n       $JPItems = $JPLib.items;\r\n       $JPCount = $JPitems.count;\r\n        \r\n          for ($itemIndex=0; $itemIndex -lt $JPCount; $itemIndex++)\r\n          {\r\n            $JPItem=$JPItems[$itemIndex];\r\n            $FName=$JPItem.file.name;\r\n             \r\n             \r\n            if ($FName -match \".pdf\") #you choose the criteria, here I match on pdfs\r\n            {\r\n                $JPFile=$JPItem.file;\r\n                Write-Host \"Deleting $($JPFile.Versions.Count), size of each: $($JPFile.properties.vti_filesize),$($JPitem.url)\"\r\n                 \r\n                $JPfile.Versions.DeleteAll()\r\n            }\r\n                         \r\n            continue; # don't mess with timestamp\/author just yet\r\n             \r\n             \r\n             [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(\r\n                {\r\n                [System.DateTime]$date = $SourceItem[\"Modified\"]\r\n                $TargetItem[\"Modified\"]=$date;\r\n                try {\r\n                $TargetItem[\"Editor\"] = $SourceItem[\"Editor\"]\r\n                }\r\n                catch\r\n                {\r\n                    write-host -foregroundcolor red \"Error could not assign editor of $($targetItem.url)\"\r\n                }\r\n                 \r\n                 \r\n                $TargetItem.update()   \r\n                try\r\n                {  # two deletes required\r\n                    $TargetItem.Versions[1].delete()\r\n                    $TargetItem.Versions[1].delete()\r\n                }\r\n                catch\r\n                {\r\n                    write-host -foregroundcolor red \"Warning: could not delete old version of $($targetItem.url)\"\r\n                }\r\n                 \r\n                })\r\n          }\r\n    }\r\n}  \r\n<\/pre>\n<div style=\"background-color: white; box-shadow: 0 0 10px whitesmoke; padding: 20px; width: 800px;\">\n<h4 style=\"color: black;\">Additional Read<\/h4>\n<p><a href=\"https:\/\/reality-tech.com\/url-to-a-location-inside-a-pdf\/\" data-uw-rm-brl=\"false\" style=\"color:#1f6799\" target=\"_blank\" rel=\"noopener\">How to Configure a URL to a Specific Location Inside a PDF<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1947,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[32],"tags":[],"class_list":["post-1943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sharepoint"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1943"}],"version-history":[{"count":5,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1943\/revisions"}],"predecessor-version":[{"id":2997,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1943\/revisions\/2997"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1947"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}