Forum Discussion

aasenomad's avatar
aasenomad
Copper Contributor
Feb 28, 2022

OneDrive List View Threshold issue in PowerShell

I've this script that delete OneDrive folder but I'm just wondering how can I modify it so that It'll be able to delete a folder that have over 5000 items/files. I know that Microsoft has 5000 items list view limit and I'm just wondering what will be my work around on this. 

Right now I'm getting "Listview threshold" error and it always skipped deleting for a folder that have over 5000 items.

 

 

#Key File information for secure connection
$Global:adminUPN = ""
$Global:PasswordFile = ""
$Global:KeyFile = ""
$Global:adminPwd = ""

#Pwd Key Encryption File
$key = Get-Content $Global:KeyFile
$Global:adminPwd = Get-Content $Global:PasswordFile | ConvertTo-SecureString -Key $key
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Global:adminUPN, $Global:adminPwd
    
#Variables


      $SiteURL = "https://companyName-my.sharepoint.com/personal/user"
      $ServerRelativeUrl= "Documents/PC--01-Dec-0257/C$"
      Write-host  $ServerRelativeUrl

        
    Try {
        #Get Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Global:adminUPN, $Global:adminPwd)
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $ctx.Credentials = $Credentials
    
        #Get the web from URL
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.executeQuery()


    
        #Get the Folder object by Server Relative URL
        $Folder = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl)
        $Ctx.Load($Folder)
        $Ctx.ExecuteQuery()
    
        #Call the function to empty Folder
        Empty-SPOFolder $Folder
    
        #Delete the given Folder itself
        Write-host  -f Green "Deleting Folder:"$Folder.ServerRelativeUrl
        $Folder.Recycle() | Out-Null
        $Ctx.ExecuteQuery()
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }


    
        #Function to Delete all files and Sub-folders of a given Folder
        Function Empty-SPOFolder([Microsoft.SharePoint.Client.Folder]$Folder) {
            Try {
                #Process all Sub Folders of the given folder
                $SubFolders = $Folder.Folders
                $Ctx.Load($SubFolders)
                $Ctx.ExecuteQuery()
                
                #Empty all subfolders
                foreach($SubDir in $SubFolders) {
                    #Exclude "Forms" and Hidden folders
                    Empty-Folder -Folder $SubDir
                }

                #delete all subfolders
                foreach($SubDir in $SubFolders) {
                    #Exclude "Forms" and Hidden folders
                
                    #Call the function recursively to empty the folder
                    Empty-SPOFolder -Folder $SubDir

                    #Delete the folder
                    Write-Host $SubDir.UniqueId -ForegroundColor Green
                    #$Ctx.Web.GetFolderById($SubDir.UniqueId).Recycle() | Out-Null
                    $Ctx.Web.GetFolderById($SubDir.UniqueId).DeleteObject() | Out-Null
                    $Ctx.ExecuteQuery()
                    Write-Host "Deleted Folder: $($SubDir.ServerRelativeUrl)" -ForegroundColor Green
                }

                #Empty the root folder
                # because we didn't reuse the $Folder variable, this is still a reference to the root folder
                Empty-Folder -Folder $Folder
                $Ctx = $Folder.Context
                $Files = $Folder.Files
                $Ctx.Load($Files)
                $Ctx.ExecuteQuery()

                #Iterate through each File in the Root folder
                $odCounter = 0
                $odLimit = 1000
                foreach($File in $Files) {
                    #Delete the file
                    Write-Host $File.Name -ForegroundColor Green
                    $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null
                    $Folder.Files.GetByUrl($File.ServerRelativeUrl).DeleteObject() | Out-Null
                    Write-Host "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'" -ForegroundColor Green
                }
                if($odCounter -gt $odLimit){
                    $Ctx.ExecuteQuery()
                }
            }
            Catch {
                # use sub-expressions $() to expand the values of the properties in the string
                Write-Host "Error: $($Folder.UniqueId) - $($File.Name) " $_.Exception.Message -ForegroundColor Red
            }
        }

        #Function to Delete all files from a folder
        Function Empty-Folder([Microsoft.SharePoint.Client.Folder]$Folder){
                Try {
                    #Get All Files from the Folder
                    $Ctx = $Folder.Context
                    $Files = $Folder.Files
                    $iterations = [Math]
                    $Ctx.Load($Files)
                    $Ctx.ExecuteQuery()
            
                    #Iterate through each File in the Root folder
                    $odCounter = 0
                    $odLimit = 1000
                    Foreach($File in $Files)
                    {
                        #Delete the file
                        Write-Host -f Green "$File.Name"
                        $Folder.Files.GetByUrl($File.ServerRelativeUrl).DeleteObject() | Out-Null
                        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
                    }
                    if($odCounter -gt $odLimit){
                        $Ctx.ExecuteQuery()
                    }
                
                }
                Catch {
                write-host -f Red "Error: $Folder.UniqueId - $File.Name " $_.Exception.Message
                }
            }