SharePoint Online: Delete Version History using PnP PowerShell

Requirement: Delete version history in SharePoint Online.

How to Delete versions in SharePoint Online?
To delete previous versions of a file in SharePoint Online, follow these steps:
  • Navigate to your SharePoint Online document library, Select the file and then click on "Version History" from the toolbar. (or Select "Version History" from the context menu of the document)
  • On the version history page, You can choose "Delete" from the context menu of the particular version. To delete all versions of a document or list item, in the Version history page, click the "Delete All Versions" link, and then confirm the prompt.
    sharepoint online delete versions powershell
SharePoint Online: Delete Version History using PnP PowerShell
Lets delete all versions of all items in a list or library using PowerShell.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName="Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Ctx= Get-PnPContext

#Get All Items from the List - Exclude 'Folder' List Items
$ListItems = Get-PnPListItem -List $ListName -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"

ForEach ($Item in $ListItems)
{
    #Get File Versions
    $File = $Item.File
    $Versions = $File.Versions
    $Ctx.Load($File)
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()
    Write-host -f Yellow "Scanning File:"$File.Name
    
    If($Versions.Count -gt 0)
    {
        #Delete all versions
        $Versions.DeleteAll()
        $Ctx.ExecuteQuery()
        Write-Host -f Green "Deleted All Previous Versions of the File:"$File.Name
    }
}

Keep Last Five Versions and Delete the Rest:
Going through version history page and deleting one by one is cumbersome. Here is the PnP PowerShell to keep last 'N' versions and delete all others.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName="Documents"
$VersionsToKeep = 5

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Ctx= Get-PnPContext

#Get All Items from the List - Exclude 'Folder' List Items
$ListItems = Get-PnPListItem -List $ListName -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"

ForEach ($Item in $ListItems)
{
    #Get File Versions
    $File = $Item.File
    $Versions = $File.Versions
    $Ctx.Load($File)
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()

    Write-host -f Yellow "Scanning File:"$File.Name
    $VersionsCount = $Versions.Count
    $VersionsToDelete = $VersionsCount - $VersionsToKeep
    If($VersionsToDelete -gt 0)
    {
        write-host -f Cyan "`t Total Number of Versions of the File:" $VersionsCount
        #Delete versions
        For($i=0; $i -lt $VersionsToDelete; $i++)
        {
            write-host -f Cyan "`t Deleting Version:" $Versions[0].VersionLabel
            $Versions[0].DeleteObject()
        }
        $Ctx.ExecuteQuery()
        Write-Host -f Green "`t Version History is cleaned for the File:"$File.Name
    }
}
This script leaves the recent 'N' number of versions and delete all previous versions of the documents.
SharePoint Online: Delete Version History using PnP PowerShell SharePoint Online: Delete Version History using PnP PowerShell Reviewed by Salaudeen Rajack on May 28, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.