Delete All Document Versions in SharePoint Document Library using PowerShell

Requirement: Delete all old document versions in a SharePoint site collection to free up some disk space occupied by document versions. Because simply disabling versioning does not delete old versions which are already created until the next update happens on the document! So, If you want to release the space occupied by previous versions, you’ll have to remove them manually.

How to delete document versions in SharePoint?

To Delete all previous versions of a document in SharePoint, follow these steps:

  1. Navigate to your SharePoint Library, click the ellipse (…), and then click on “Version History”.
  2. From the version history page, click on the “Delete all versions” link, and confirm if prompted!
    powershell to delete all document versions in sharepoint

PowerShell to delete all Document Versions in SharePoint:

Here is the PowerShell script to clear all version history from all documents from a SharePoint document library.

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
 
#Configuration Parameters
$WebURL = "https://portal.crescent.com.com/projects/"
$ListName = "Documents"

#Get the Web, List objects
$Web= Get-SPWeb $WebURL
$List= $web.Lists[$ListName]

#Get all Items from the List
$ListItems = $List.Items
Write-host "Total Items Found in the List:"$List.ItemCount
$Counter =0

#Query to batch process
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.ViewAttributes = "Scope='Recursive'"
$Query.RowLimit = "2000"

Do {
    #Get List Items defined by the Query
    $ListItems = $List.GetItems($Query)
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
	
    #Iterate through each item
	ForEach ($ListItem  in $ListItems)
	{
		#Display Progress bar
		$Counter=$Counter+1    
		 Write-Progress -Activity "Cleaning up versions" -Status "Processing Item:$($ListItem['Name']) $($counter) of $($List.ItemCount)" -PercentComplete $($Counter/$List.ItemCount*100)
		 
		#If the File has versions, clean it up
		if ($ListItem.Versions.Count -gt 1)
		{ 
			Write-host "Cleaning versions for: " $ListItem["Name"]
			$ListItem.file.Versions.DeleteAll()
		}
	}

} While ($Query.ListItemCollectionPosition -ne $Null)

SharePoint: PowerShell to Delete All Versions for the entire site collection

How about deleting document versions for all document libraries in a site collection? Here is the PowerShell script to delete all document versions in a site collection:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Site Collection
$site = Get-SPSite "https://sharepoint.crescent.com/sites/operations"

#Loop through all sites in the site collection
foreach($web in $site.AllWebs)
{
  #Iterate through all Lists
  foreach($List in $Web.Lists)
  { 
      #Get only document libraries & Skip Hidden 
      if( ($List.BaseType -eq "DocumentLibrary") -and ($List.EnableVersioning) -and ($List.Hidden -eq $false) -and($List.IsApplicationList -eq $false) ) 
      {   
          #loop through each item
          foreach ($item in $list.Items)
          {
             if($item.File.Versions.Count -gt 0)
             {
                # delete all versions
                Write-Host "Deleting $($item.File.Versions.Count) Version(s) on: $($web.URL)$($item.URL)"
                $item.file.Versions.DeleteAll()
             }
          }
      }
   }
 }
 
$site.Dispose()
Write-Host "Script execution Completed!"

This PowerShell script will delete old versions in SharePoint document libraries for the entire site collection.

Related posts:

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

4 thoughts on “Delete All Document Versions in SharePoint Document Library using PowerShell

Leave a Reply

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