
How To Delete Version History in SharePoint Online using PnP PowerShell
Learn how to delete version history in SharePoint Online using PnP PowerShell, streamlining your document library and enhancing performance.
Introduction
Version history in SharePoint Online allows users to keep track of changes made to files over time. While versioning can be useful, it can also lead to a bloated document library with multiple versions of files. If you want to clean up and delete the version history of files in SharePoint Online using PowerShell, the SharePoint Patterns and Practices (PnP) PowerShell module provides a convenient way to achieve this. In this article, we will guide you through the process of deleting version history using SharePoint Online PnP PowerShell.
Before we proceed, please ensure that you have the following prerequisites in place:
Uninstall the Legacy SharePoint PowerShell Module
1. Open a PowerShell command prompt with administrative privileges. To do this, right-click on the Start button and select “Windows PowerShell (Admin)” or “Windows PowerShell” if you’re using an older version of Windows.
2. To uninstall the module, use the following command
Uninstall-Module -Name Microsoft.SharePoint.PowerShell Uninstall-Module -Name Microsoft.OnlineSharePoint.PowerShell
3. Confirm the uninstallation by typing “Y” or “A” when prompted.
4. That’s it! The Legacy SharePoint PowerShell module should now be uninstalled from your system.
Installing the PowerShell 7.0 MSI package

To install PowerShell on Windows, use the following links to download the install package from GitHub.
Install Latest SharePoint Online PNP PowerShell
Install the New PnP PowerShell Module by running the command in PowerShell 7.0
Install-Module PnP.PowerShell
Once you have the prerequisites ready, follow the steps below.
Step 1: Register Azure AD Application and Grant Access to the Tenant
1. Register an Azure AD Application in the Azure portal.
Register-PnPManagementShellAccess
2. Grant appropriate permissions to the registered application to access SharePoint Online.
Step 2: Set up the Delete Version History SharePoint Online PNP PowerShell Script
1. Copy and paste the following script into the editor:
# Parameters $SiteURL = "https://PatrickDomingues.sharepoint.com/sites/MySite" $LibraryName = "Document Library Name" # Function to clear all file versions folder by folder within the library. Function Cleanup-Versions([Microsoft.SharePoint.Client.Folder]$Folder) { Write-host "Processing Folder:" $Folder.ServerRelativeUrl -f Yellow # Get the Site Relative URL of the folder $FolderSiteRelativeURL = $Folder.ServerRelativeURL.Replace($Web.ServerRelativeURL, [string]::Empty) # Get all files from the folder $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File # Iterate through each file ForEach ($File in $Files) { # Get file versions $Versions = Get-PnPProperty -ClientObject $File -Property Versions Write-host -f White "`tScanning File:" $File.Name If ($Versions.Count -gt 0) { # Delete all versions $Versions.DeleteAll() Invoke-PnPQuery Write-Host -f Green "`t`tPrevious Versions of the File Deleted:" $File.Name } } # Get sub-folders from the folder - Exclude "Forms" and hidden folders $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))} Foreach ($SubFolder in $SubFolders) { # Call the function recursively Cleanup-Versions -Folder $SubFolder } } # Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive $Web = Get-PnPWeb # Get the root folder of the library $RootFolder = Get-PnPList -Identity $LibraryName -Includes RootFolder | Select -ExpandProperty RootFolder # Call the function with the root folder of the library Cleanup-Versions -Folder $RootFolder
Step 3: Customize the SharePoint Online PNP PowerShell Script
- Replace the
$SiteURL
variable value with the URL of your SharePoint site where the document library is located. - Replace the
$LibraryName
variable value with the name of your document library.
Step 4: Execute the SharePoint Online PNP PowerShell Script
- Save the script with a
.ps1
extension (e.g.,SharePointDeleteVersionHistory.ps1
). - Open PowerShell and navigate to the folder where the script is saved.
- Run the script by executing the command
.\SharePointDeleteVersionHistory.ps1
.
The script will connect to the specified SharePoint Online site, traverse through the document library, and delete the version history for each file. It will also recursively clean up version history in sub-folders, excluding the “Forms” folder and hidden folders.
Please note that executing the script will permanently delete all previous versions of files in the specified document library. Make sure to double-check the site URL and document library name before running the script.
In conclusion, using SharePoint Online PnP PowerShell, you can easily delete version history and optimize your document library’s storage. This allows you to keep your SharePoint environment clean and organized, improving overall performance and user experience.
Hey there! Came across your post on the WordPress feed and couldn’t resist saying hello. I’m already hooked and eagerly anticipating more captivating posts. Can’t seem to find the follow button, haha! Guess I’ll have to bookmark your blog instead. But rest assured, I’ll be eagerly watching for your updates!
Thanks – TheDogGod
Thank you for the kind words. I hope this script makes your life easier as it did for me.
————–
Wow, this is exactly what I’ve been looking for! Thank you so much for sharing this helpful information. I can’t wait to try out these steps and streamline my SharePoint Online version history. Great post!
Thank you for the visit.
This script has come handy many times to clear up space in SharePoint. I hope you enjoy it as much as I did!
Nice!!
Is there a way of doing this so you can select an individual folder?
I keep getting disconnected with a 429 error so cant get through the entire library in one go without being throttled.
Hello, Yes, I noticed that too. I got throttled as well but not disconnected, it took 1 whole day to go through 3tb of data. You might be able to combat the throttle by adding some sort of timer before jumping to the next file.
I think there is a way to select a root folder within a specific library. I will have to play around with it.
Not so sure why my comment was deleted, but I was wondering if there is an option to delete the versions and leave the last 5 versions of the files?
Thanks,
Martin
Hello Martin,
https://patrickdomingues.com/2023/08/18/how-to-cleanup-sharepoint-files-retaining-only-the-latest-file-versions-with-powershell/
You can modify the version count from 1 to 5.
HI Patrick,
This is amazing! It worked! Thanks a lot.
Now do you have a script to delete previous version(and leave 5) for a specific folder in a library? That would help a lot
Thanks,
Martin
This is an excellent script thank you so much for sharing! Now I’m trying to figure out how to set a cut-off date for the versions I’m keeping in a particular library… There has to be a way to do it. 🙂
Thanks again – Extremely helpful!
Mike
Do you believe that a cutoff date would be beneficial?
Depending on your SharePoint settings if not changed you could have up to 500 revisions of 1 file, let’s say if that 1 file is 10mb, and 500 revisions were all in 1 week.
I don’t consider revisions in SharePoint as backups; they are more like autosave points in time. If I really need to restore a file, I will grab it from my Microsoft 365 backup solution instead of SharePoint eating up expensive production storage.
Just my opinion.
Thank you,
Patrick Domingues