We have script to delete versions for whole document list. Need to edit it to delete only from a specific folder within the list since we get error when deleting as "threshold limit exceeded"

Nimesh Samaratunga 20 Reputation points
2023-02-28T02:54:42.0866667+00:00
#Config Variables
$SiteURL = "https://zqh7j.sharepoint.com/sites/test"
$ListName="Documents"
$VersionsToKeep = 2

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#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
    }
}

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,476 Reputation points Microsoft External Staff
    2023-02-28T05:58:40.88+00:00

    Hi @Anonymous

    The codeproject article referenced in the question is apparently using either an incorrect or outdated schema for the CAML.
    When you set the ViewXml property of the CamlQuery, the top-level XML element should be the <View>. The <Query> is one of the optional child elements. SharePoint was effectively ignoring your <RowLimit> element (rather than perhaps throwing a helpful exception about XML schema), thus resulting in the List View Threshold exception.

    We can fetch all the items(include folders) and keep it in collection object and query this collection object using Linq expression. Please refer to following script

    #Get All Items from the List 
    $ListItems = Get-PnPListItem -List $ListName -Query "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"
    
    ForEach ($Item in $ListItems)
    {
        Exclude 'Folder' List Items
        If($Item.FileSystemObjectType = 0){
            #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
            }
        } 
    }
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. MB 0 Reputation points
    2024-09-27T14:59:05.3533333+00:00

    Sorry for posting here but your post and comments are related with what I need to accomplish.

    I'm searching for a solution to point out to our SysAdmins for a script that does the following in bulk for a specific folder, all subfolders and all files within a Sharepoint location:

    • evaluate version date/hour of all files, while keeping always the 1st version, the later version of each day until a specific predefined date, delete all version in between.

    Also, some unintended usage of a copy batch on a desktop synced hundreds of version copies with the exact same date/hour size for thousands of files that need to be deleted.

    Is this possible by modifying this script?

    Thanks in advance for any help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.