Forum Discussion

rohankapz's avatar
rohankapz
Copper Contributor
Sep 28, 2023

Powershell script to delete all versions history in Sharepoint Site

Hi All,

 

I have a SharePoint script which is designed to delete all version history for all files in a SharePoint site but keep the 10 latest versions of the files. Script is attached but i am always getting this error when running the script.  Site URL is correct and the user profile has Site Owner/Admin privileges at full control on the SharePoint site. Please can we get some help asap as we have 50 GB left in our ORG and are loosing 10 GB Per day and by end of this week we will run of SharePoint storage even though we have 2.4 TB

 

 


MethodInvocationException:
Line |
50 | $ctx.ExecuteQuery()
| ~~~~~~~~~~~~~~~~~~~
| Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
MethodInvocationException:
Line |
31 | $ctx.ExecuteQuery()
| ~~~~~~~~~~~~~~~~~~~
| Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
OperationStopped:
Line |
33 | foreach ($file in $folder.Files) {
| ~~~~~
| 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.
MethodInvocationException:
Line |
40 | $ctx.ExecuteQuery()
| ~~~~~~~~~~~~~~~~~~~
| Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
OperationStopped:
Line |
42 | foreach ($subFolder in $folder.Folders) {
| ~~~~~~~~~~
| 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.
Completed. Kept the latest 10 versions for all files in the site.
PS C:\PowerShell-7.3.7-win-x64>

 

7 Replies

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    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)

    • rohankapz's avatar
      rohankapz
      Copper Contributor
      Hi LeonPavesic ,

      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.?
      • LeonPavesic's avatar
        LeonPavesic
        Silver 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-SPOService

         

        To 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:

        1. Open PowerShell with elevated privileges.
        2. Run the following command:
         

         

        Install-Module PnP.PowerShell -Scope CurrentUser

         

        This 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 -Force

         

         

        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)

Resources