kwizcom banner advertisement

SharePoint Online: Delete Version History using Powershell CSOM

How to delete version history in SharePoint online? 
To Delete all previous versions of a document in SharePoint, follow these steps:
  • Navigate to your SharePoint Library, Select the file and then click on "Version History" from the ribbon menu.
    sharepoint online delete version history
  • From the version history page, click on "Delete all versions" link, confirm if prompted!sharepoint online delete versions

SharePoint Online: Delete Version History using PowerShell
Case 1: Delete All Versions for a particular file:
This PowerShell script deletes all versions in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$FileURL="/Sites/Sales/ProjectDocuments/ProjectMetrics.xlsx"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

    #Get all versions of the file
    $Versions = $File.Versions
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()

    Write-host -f Yellow "Total Number of Versions Found :" $Versions.count

    #Delete all versions of the file
    $Versions.DeleteAll()
    $Ctx.ExecuteQuery()

    Write-host -f Green "All versions Deleted for given File!"   
}
Catch {
    write-host -f Red "Error deleting versions!" $_.Exception.Message
}

Case 2: Delete All Versions for All Files in a Library:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$LibraryName="Project Docs"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
 
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)
  
    #Get all List items from the library - Exclude "Folder" objects
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()  
   
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    {       
        #Get all versions of the file
        $Ctx.Load($Item.File)        
        $Ctx.Load($Item.File.Versions)
        $Ctx.ExecuteQuery()
        Write-host "Processing Item:" $Item.file.Name
 
        #Delete all versions of the file
        If($Item.File.Versions.count -gt 0)
        {
            $Item.File.Versions.DeleteAll()
            $Ctx.ExecuteQuery()
            Write-host -f Green "All Versions deleted on "$Item.file.Name
        }
    }    
}
Catch {
    write-host -f Red "Error Deleting version History!" $_.Exception.Message
}

Case 3: Leave Last Five versions and Delete the Rest in the Library:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$LibraryName="Project Documents"
$VersionsToKeep=5

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)
 
    #Get all items from the library
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
 
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    {
        #Get all versions of the file
        $Versions = $Item.File.Versions
        $Ctx.Load($Versions)
        $Ctx.Load($Item.File) #To get File Name
        $Ctx.ExecuteQuery()

        Write-host -f Yellow "Total Number of Versions Found in '$($Item.File.Name )' : $($Versions.count)"

            #Check if number of versions are more than limit
            While($Item.File.Versions.Count -gt $VersionsToKeep)
            {
                write-host "Deleting Version:" $Versions[0].VersionLabel
                $Versions[0].DeleteObject()
                $Ctx.ExecuteQuery()
    
                #Reload versions
                $Ctx.Load($Item.File.Versions)
                $Ctx.ExecuteQuery()
            }
    }
}
Catch {
    write-host -f Red "Error deleting versions!" $_.Exception.Message
}
If you want to keep last five versions and delete the rest for a particular file, use:
    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

    #Get all versions of the file
    $Versions = $File.Versions
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()
    $Versionscount = $Versions.count

    Write-host -f Yellow "Total Number of Versions Found :" $Versionscount

    #Check if number of versions are more than limit
    While($File.Versions.Count -gt $VersionsToKeep)
    {
        write-host "Deleting Version:" $Versions[0].VersionLabel
        $Versions[0].DeleteObject()
        $Ctx.ExecuteQuery()
    
        #Reload versions
        $Ctx.Load($File.Versions)
        $Ctx.ExecuteQuery()
    }
You can also delete a particular version by its Label as: $Versions.DeleteByLabel($LabelID)
SharePoint Online: Delete Version History using Powershell CSOM SharePoint Online: Delete Version History using Powershell CSOM Reviewed by Salaudeen Rajack on February 23, 2016 Rating: 5

10 comments:

  1. Hi Salaudeen,
    These are really great and helpful scripts.
    I'm using the script to delete all but 5 versions for all files in a library, but the script fails if the library has any sub-folders. Any ideas what could be added so that the sub-folders are ignored? Thanks!

    ReplyDelete
    Replies
    1. We've to use CAML query to filter Folder Objects. Refer Case #2

      Delete
  2. Does Case 2 cover all files in all folders? and is it possible to have it filtered to limit rows to 5000 on large sites ?
    Thanks !

    ReplyDelete
  3. I am getting an Error that I do not know how to fix...…..could someone help: --- Error deleting versions! Exception calling "ExecuteQuery" with "0" argument(s): "Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "File

    ReplyDelete
    Replies
    1. Script has been updated in CAML query to skip Folder objects. Try now!

      Delete
    2. Salaudeen, Thanks for your quick. This is very helpful but I think many of us need help as our users keep adding files to multiple folders. Is possible to add a script targeting a specific folder and set it up as part of the variables. I believer We have admins here with multiple folders as me with more than 1000 items and set up to 2 or 3 versions that we are trying to keep "LEAN"........if Possible We will Appreciate the help.

      Delete
    3. Simple! Just append this line after line#25 in the case 3:
      $Query.FolderServerRelativeUrl="/Project Docs/Active" (or whatever sub-folder path)

      Delete
    4. This comment has been removed by the author.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.