Forum Discussion
Powershell script to delete all versions history in Sharepoint Site
Hi rohankapz,
The error message The remote server returned an error: (400) Bad Request. can occur for a number of reasons, such as:
- The SharePoint site URL is incorrect.
- The user account does not have the necessary permissions to delete version history.
- There is a problem with the SharePoint site.
You can try this PowerShell script to delete all version history in Sharepoint Site:
# Import the SharePoint Client Library
Import-Module SharePointPnPPowerShellOnline
# Connect to the SharePoint site
$siteUrl = "https://<your-sharepoint-site-url>"
Connect-SPOService -Url $siteUrl
# Get the root folder of the SharePoint site
$rootFolder = Get-SPOFolder -Url $siteUrl
# Delete all version history for all files in the SharePoint site
foreach ($file in $rootFolder.Files) {
$file.VersionHistory.DeleteAll()
$ctx.ExecuteQuery()
}
# Delete all version history for all subfolders in the SharePoint site
foreach ($subFolder in $rootFolder.Folders) {
Delete-SPOFolderVersionHistory -FolderUrl $subFolder.Url
}
# Disconnect from the SharePoint site
Disconnect-SPOService
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)
Can i have an updated script which keep the 10 latest versions. I have full control permissions on the site and SharePoint URL is correct.?
- LeonPavesicSep 29, 2023Silver Contributor
Hi rohankapz,
you can try to use this script:
# Import the SharePoint Client Library Import-Module SharePointPnPPowerShellOnline # Connect to the SharePoint site $siteUrl = "https://<your-sharepoint-site-url>" Connect-SPOService -Url $siteUrl # Get the root folder of the SharePoint site $rootFolder = Get-SPOFolder -Url $siteUrl # Get all the files in the root folder $files = $rootFolder.Files # Iterate through each file and delete all versions but the latest 10 foreach ($file in $files) { $versionHistory = $file.VersionHistory # Sort the versions by creation date, descending $versions = $versionHistory.OrderByDescending({ $_.CreationTime }) # Keep the latest 10 versions and delete the rest $versionsToDelete = $versions | Select-Object -Skip 10 foreach ($versionToDelete in $versionsToDelete) { $versionToDelete.Delete() } # Execute the changes $ctx.ExecuteQuery() } # Disconnect from the SharePoint site Disconnect-SPOServiceTo use this script, simply replace the <your-sharepoint-site-url> placeholder with the URL of your SharePoint site. Then, run the script in elevated PowerShell or you can use SharePoint Online Management Shell:
Download SharePoint Online Management Shell from Official Microsoft Download Center
It is important to have the latest version of the SharePoint PnP PowerShell Online module.To install the latest SharePoint PnP PowerShell Online module, you can use the following steps:
- Open PowerShell with elevated privileges.
- Run the following command:
Install-Module PnP.PowerShell -Scope CurrentUserThis will install the latest stable version of the SharePoint PnP PowerShell Online module for the current user.
Once the module is installed, you can import it into your PowerShell session using the following command:
Import-Module PnP.PowerShell
If you are already using the SharePointPnPPowerShellOnline module, you can uninstall it before installing the PnP.PowerShell module. To uninstall the SharePointPnPPowerShellOnline module, run the following command:Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -ForcePlease click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)- rohankapzSep 29, 2023Copper Contributor
Hi LeonPavesic,
I was able to add the modules and run the Connect SPO command, but when i try to run the script you provided i get the following errors.PS C:\Windows\system32> # Import the SharePoint Client Library
Import-Module SharePointPnPPowerShellOnline# Connect to the SharePoint site
$siteUrl = "https://essensysuk.sharepoint.com/sites/Rohantest"
Connect-SPOService -Url $siteUrl# Get the root folder of the SharePoint site
$rootFolder = Get-SPOFolder -Url $siteUrl# Get all the files in the root folder
$files = $rootFolder.Files# Iterate through each file and delete all versions but the latest 10
foreach ($file in $files) {
$versionHistory = $file.VersionHistory# Sort the versions by creation date, descending
$versions = $versionHistory.OrderByDescending({ $_.CreationTime })# Keep the latest 10 versions and delete the rest
$versionsToDelete = $versions | Select-Object -Skip 10
foreach ($versionToDelete in $versionsToDelete) {
$versionToDelete.Delete()
}# Execute the changes
$ctx.ExecuteQuery()
}# Disconnect from the
WARNING: The names of some imported commands from the module 'SharePointPnPPowerShellOnline' include unapproved verbs that mi
ght make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Ver
bose parameter. For a list of approved verbs, type Get-Verb.
Connect-SPOService : No valid OAuth 2.0 authentication session exists
At line:6 char:1
+ Connect-SPOService -Url $siteUrl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-SPOService], AuthenticationException
+ FullyQualifiedErrorId : Microsoft.Online.SharePoint.PowerShell.AuthenticationException,Microsoft.Online.SharePoint.Po
werShell.ConnectSPOService
Get-SPOFolder : The term 'Get-SPOFolder' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:9 char:15
+ $rootFolder = Get-SPOFolder -Url $siteUrl
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-SPOFolder:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be
explicitly requested.
At line:15 char:10
+ foreach ($file in $files) {
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException- LeonPavesicSep 29, 2023Silver Contributor
Hi rohankapz,
I'm glad you were able to install the modules and connect to your SharePoint site.
However, I'm sorry to hear that you're getting errors when trying to run the script I provided.Here are some troubleshooting tips:
Make sure you're using the latest version of the SharePoint PnP PowerShell Online module. You can check this by running the following command:
Get-Module SharePointPnPPowerShellOnlineThe version number should be at least 1.24.0. If it's not, you can update the module using the following command:
Update-Module SharePointPnPPowerShellOnline -Scope CurrentUserMake sure you're using the correct URI syntax for your SharePoint site. The error message you're getting indicates that the URI is invalid. Try using the following format:
https://<your-sharepoint-tenant-name>.sharepoint.com/<your-site-name>
Make sure the Get-SPOFolder cmdlet is available. This cmdlet is not included in the PnP.PowerShell module by default. To install it, run the following command:
Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUserThe warning message you are seeing is because the SharePoint PnP PowerShell Online module includes some commands with unapproved verbs. To find the commands with unapproved verbs, run the following command:
Import-Module SharePointPnPPowerShellOnline -Verbose
Then try to use the script one more time.# Import the SharePoint Client Library Import-Module SharePointPnPPowerShellOnline # Connect to the SharePoint site $siteUrl = "https://essensysuk.sharepoint.com/sites/Rohantest" Connect-SPOService -Url $siteUrl # Get the root folder of the SharePoint site $rootFolder = Get-SPOFolder -Url $siteUrl # Get all the files in the root folder $files = $rootFolder.Files # Iterate through each file and delete all versions but the latest 10 foreach ($file in $files) { $versionHistory = $file.VersionHistory # Sort the versions by creation date, descending $versions = $versionHistory.OrderByDescending({ $_.CreationTime }) # Keep the latest 10 versions and delete the rest $versionsToDelete = $versions | Select-Object -Skip 10 foreach ($versionToDelete in $versionsToDelete) { $versionToDelete.Delete() } # Execute the changes $ctx.ExecuteQuery() } # Disconnect from the SharePoint site Disconnect-SPOServicePlease click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic (LinkedIn)