Delete Item Versions In SharePoint Programmatically

Versioning in SharePoint is an awesome feature that lets users create versions of their documents. However, when left unchecked, especially when the below category of “Document Version History” is selected – an end user will be able to create practically an indefinite number of versions. This is not a recommended best practice and may end up using a huge amount of storage over the years.

SharePoint

While you can always enable versioning settings, and delete the older versions manually, this will be a very cumbersome task, for libraries/lists with more than 1000s of list items.

The script below takes care of that! This script will be helpful in identifying the list of the versions, which needs to be deleted, based on dates of creation, and other input parameters.

Modes and Usage of the Script

This script has three modes,

  • Delete item versions in SharePoint using PowerShell – Report Mode
    This gives the report of the item versions that will be deleted but will not delete anything. This is useful for analyzing the files before the decision is  made for deletion.
  • Delete item versions in SharePoint using PowerShell – Recycle Mode
    This mode will send the identified versions to the Recycle Bin rather than deleting them permanently. The report generated at the end will indicate the item versions moved to the Recycle Bin
  • Delete item versions in SharePoint using PowerShell – Permanent Delete Mode
    This mode will delete the identified versions permanently. Once deleted, the versions cannot be retrieved. The report generated at the end will indicate the item versions that have been successfully deleted.

However, it is to be noted that the current version of the document (Current Major Version and current Minor Version) will not be deleted, even if it falls into the category.  This script will also let us users know of the storage size saved by means of deletion of the versions.

SharePoint

Input Parameters

The script takes in three input parameters, with sample examples are,

$myWeb"http://mysharepoint.com/sites/myweb1"The URL of the web or subsite where the list/library is located
$myList"Fantastic Library"The name of the list/library
$myDate"2014-01-01"The date beyond each which the versions are to be deleted
$myPath "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv" The location and name of the CSV report

Code for Delete item versions in SharePoint using PowerShell – Report Mode

  1. ###########################################################################  
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Report Mode ####  
  3. ###########################################################################  
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)   
  5. {  
  6.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  7. }  
  8.   
  9. $myWeb = "http://mysharepoint.com/sites/myweb1"  
  10. $myList = "Fantastic Library"  
  11. $myDate = "2014-01-01"  
  12. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"  
  13.   
  14. $results = @()  
  15. $storageSaved = 0  
  16.   
  17. $SPWeb = Get-SPWeb $myWeb  
  18. foreach ($list in $SPWeb.Lists)  
  19. {  
  20.     if ($list.Title -eq $myList)  
  21.     {  
  22.         Write-Host "Name: " $list.Title  
  23.         Write-Host "Type: " $list.BaseType  
  24.         $itemColl = $list.Items  
  25.         foreach ($item in $itemColl)  
  26.         {             
  27.             foreach ($ver in $item.Versions)  
  28.             {  
  29.                 #this block for non-current versions  
  30.                 if (!$ver.IsCurrentVersion)          
  31.                 {   
  32.                   if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
  33.                   {                                        
  34.                         $RowDetails = @{  
  35.                             "Document Name" = $item.Name  
  36.                             "Version Number" = $ver.VersionLabel  
  37.                             "Created Date" = $ver.Created  
  38.                             "Created By" = $ver.CreatedBy  
  39.                             "Delete Flag" = "Can be deleted"   
  40.                             "Date Flag" = "Match"      
  41. "Version Size (Kb)" =  "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                       
  42.                         }  
  43.                         $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)  
  44.                         $storageSaved = $storageSaved + $tempSize    
  45.                         $results += New-Object PSObject -Property $RowDetails   
  46.                    }  
  47.                    else  
  48.                    {  
  49.                         $RowDetails = @{  
  50.                             "Document Name" = $item.Name  
  51.                             "Version Number" = $ver.VersionLabel  
  52.                             "Created Date" = $ver.Created  
  53.                             "Created By" = $ver.CreatedBy  
  54.                             "Delete Flag" = "Do not Delete"   
  55.                             "Date Flag" = "Does not Match"   
  56.                 "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                          
  57.                          }  
  58.                          $results += New-Object PSObject -Property $RowDetails                           
  59.                    }  
  60.                 }  
  61.                  
  62.                 #this block for current versions  
  63.                 else  
  64.                 {  
  65.                     if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
  66.                     {    
  67.                         $RowDetails = @{  
  68.                                 "Document Name" = $item.Name  
  69.                                 "Version Number" = $ver.VersionLabel  
  70.                                 "Created Date" = $ver.Created  
  71.                                 "Created By" = $ver.CreatedBy       
  72.                                 "Delete Flag" = "Do not Delete "   
  73.                                 "Date Flag" = "Match"      
  74.                 "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)              
  75.                             }  
  76.                             $results += New-Object PSObject -Property $RowDetails   
  77.                     }  
  78.                     else  
  79.                     {  
  80.                         $RowDetails = @{  
  81.                                 "Document Name" = $item.Name  
  82.                                 "Version Number" = $ver.VersionLabel  
  83.                                 "Created Date" = $ver.Created  
  84.                                 "Created By" = $ver.CreatedBy     
  85.                                 "Delete Flag" = "Do not Delete"   
  86.                                 "Date Flag" = "Does not Match"    
  87.         "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                      
  88.                             }  
  89.                          $results += New-Object PSObject -Property $RowDetails                          
  90.                     }  
  91.                 }  
  92.                   
  93.             }  
  94.               
  95.         }  
  96.     }      
  97. }  
  98. $TotalSizeSaved = @{  
  99.     "Document Name" = ""  
  100.     "Version Number" = ""  
  101.     "Created Date" = ""  
  102.     "Created By" = ""    
  103.     "Delete Flag" = ""  
  104.     "Date Flag" = ""  
  105.     "Version Size (Kb)" =  "Total Size = " + $storageSaved   
  106. }  
  107. $results += New-Object PSObject -Property $TotalSizeSaved  
  108. $results | export-csv -Path $myPath -NoTypeInformation  
  109. Write-Host "---------------------------------------------------------------"  
  110. Write-Host $storageSaved " MB can be saved from this library"  
  111. Write-Host "---------------------------------------------------------------"   

Code for Delete item versions in SharePoint using PowerShell – Recycle Mode

  1. #################################################################  
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Recycle Mode ####  
  3. #################################################################  
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)   
  5. {  
  6.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  7. }  
  8.   
  9. $myWeb = "http://mysharepoint.com/sites/myweb1"  
  10. $myList = "Fantastic Library"  
  11. $myDate = "2014-01-01"  
  12. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"  
  13.   
  14. $results = @()  
  15. $storageSaved = 0  
  16.   
  17. $SPWeb = Get-SPWeb $myWeb  
  18. foreach ($list in $SPWeb.Lists)  
  19. {  
  20. if ($list.Title -eq $myList)  
  21. {  
  22.     Write-Host "Name: " $list.Title  
  23.     Write-Host "Type: " $list.BaseType  
  24.     $itemColl = $list.Items  
  25.     foreach ($item in $itemColl)  
  26.     {             
  27.     foreach ($ver in $item.Versions)  
  28.     {  
  29.     #this block for non-current versions  
  30.     if (!$ver.IsCurrentVersion)          
  31.     {   
  32.         if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
  33.         {                                        
  34.             $RowDetails = @{  
  35.                 "Document Name" = $item.Name  
  36.                 "Version Number" = $ver.VersionLabel  
  37.                 "Created Date" = $ver.Created  
  38.                 "Created By" = $ver.CreatedBy  
  39.                 "Status" = "Moved to Recycle Bin"   
  40.                 "Date Flag" = "Match"      
  41.                 "Version Size (Kb)" =  "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                       
  42.             }  
  43.             $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)  
  44.             $storageSaved = $storageSaved + $tempSize    
  45.             $results += New-Object PSObject -Property $RowDetails   
  46.             $ver.Recycle()  
  47.         }  
  48.         else  
  49.         {  
  50.             $RowDetails = @{  
  51.                 "Document Name" = $item.Name  
  52.                 "Version Number" = $ver.VersionLabel  
  53.                 "Created Date" = $ver.Created  
  54.                 "Created By" = $ver.CreatedBy  
  55.                 "Delete Flag" = "Ignored as Date does not match"   
  56.                 "Date Flag" = "Does not Match"   
  57.                 "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                          
  58.             }  
  59.             $results += New-Object PSObject -Property $RowDetails                           
  60.         }  
  61.     }                  
  62. }              
  63. }  
  64. }      
  65. }  
  66.   
  67.   
  68. $TotalSizeSaved = @{  
  69.    "Document Name" = ""  
  70.     "Version Number" = ""  
  71.     "Created Date" = ""  
  72.     "Created By" = ""    
  73.     "Delete Flag" = ""  
  74.     "Date Flag" = ""  
  75.     "Version Size (Kb)" =  "Total Size = " + $storageSaved   
  76. }  
  77. $results += New-Object PSObject -Property $TotalSizeSaved  
  78. $results | export-csv -Path $myPath -NoTypeInformation  
  79. Write-Host "---------------------------------------------------------------"  
  80. Write-Host $storageSaved " MB can be saved from this library"  
  81. Write-Host "---------------------------------------------------------------"   

Delete item versions in SharePoint using PowerShell – Permanent Delete Mode

  1. #################################################################  
  2. #### Delete-Item-Versions-in-SharePoint using PowerShell - Recycle Mode ####  
  3. #################################################################  
  4. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)   
  5. {  
  6.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  7. }  
  8.   
  9. $myWeb = "http://mysharepoint.com/sites/myweb1"  
  10. $myList = "Fantastic Library"  
  11. $myDate = "2014-01-01"  
  12. $myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"  
  13.   
  14. $results = @()  
  15. $storageSaved = 0  
  16.   
  17. $SPWeb = Get-SPWeb $myWeb  
  18. foreach ($list in $SPWeb.Lists)  
  19. {  
  20. if ($list.Title -eq $myList)  
  21. {  
  22.     Write-Host "Name: " $list.Title  
  23.     Write-Host "Type: " $list.BaseType  
  24.     $itemColl = $list.Items  
  25.     foreach ($item in $itemColl)  
  26.     {             
  27.     foreach ($ver in $item.Versions)  
  28.     {  
  29.     #this block for non-current versions  
  30.     if (!$ver.IsCurrentVersion)          
  31.     {   
  32.         if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
  33.         {                                        
  34.             $RowDetails = @{  
  35.                 "Document Name" = $item.Name  
  36.                 "Version Number" = $ver.VersionLabel  
  37.                 "Created Date" = $ver.Created  
  38.                 "Created By" = $ver.CreatedBy  
  39.                 "Status" = "Deleted Successfully"   
  40.                 "Date Flag" = "Match"      
  41.                 "Version Size (Kb)" =  "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                       
  42.             }  
  43.             $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)  
  44.             $storageSaved = $storageSaved + $tempSize    
  45.             $results += New-Object PSObject -Property $RowDetails   
  46.             $ver.Delete()  
  47.         }  
  48.         else  
  49.         {  
  50.             $RowDetails = @{  
  51.                 "Document Name" = $item.Name  
  52.                 "Version Number" = $ver.VersionLabel  
  53.                 "Created Date" = $ver.Created  
  54.                 "Created By" = $ver.CreatedBy  
  55.                 "Delete Flag" = "Ignored as Date does not match"   
  56.                 "Date Flag" = "Does not Match"   
  57.                 "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                          
  58.             }  
  59.             $results += New-Object PSObject -Property $RowDetails                           
  60.         }  
  61.     }                  
  62. }              
  63. }  
  64. }      
  65. }  
  66.   
  67.   
  68. $TotalSizeSaved = @{  
  69.     "Document Name" = ""  
  70.     "Version Number" = ""  
  71.     "Created Date" = ""  
  72.     "Created By" = ""    
  73.     "Delete Flag" = ""  
  74.     "Date Flag" = ""  
  75.     "Version Size (Kb)" =  "Total Size = " + $storageSaved   
  76. }  
  77. $results += New-Object PSObject -Property $TotalSizeSaved  
  78. $results | export-csv -Path $myPath -NoTypeInformation  
  79. Write-Host "---------------------------------------------------------------"  
  80. Write-Host $storageSaved " MB can be saved from this library"  
  81. Write-Host "---------------------------------------------------------------"  

Output

It is recommended that these codes are run using the system account, or by any user who has sufficient privileges in the site. Once completed, the report log will be generated in the path specified.

SharePoint

The PowerShell codes for this can also be found in my GitHub repo.