How to delete OneDrive folder in batch using PowerShell

Aase Nomad 246 Reputation points
2022-02-21T22:55:50.037+00:00

A particular user have a very large OneDrive folder with nested subfolders so I'm just wondering how can I modify my current script so that it'll be able to delete large folder with so many nested folders inside it.

Right now it's working fine for a folder that does't have too much nested folder and too much large items but it's not working if it's has too many nested subfolders with many large items.

The recomendation I get from internet is try to delete in batches but not really sure how to modify my current scripts to do that it'll also not give me threshold issue while it executing.

#Variables
$SiteURL = "https://companyName-my.sharepoint.com/personal/dz6pf0_nam_corp_gm_com"
$ServerRelativeUrl= "Documents/PC-OFFBOARD_USMPGWNCAT15C61-GZJY8T-01-Dec-0257/C$/AVL"
$BatchSize = 500


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 {
        #Get All Files from the Folder
        $Ctx = $Folder.Context
        $Files = $Folder.Files
        $Ctx.Load($Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the Root folder
        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)'"
        }
        $Ctx.ExecuteQuery()

        #Process all Sub Folders of the given folder
        $SubFolders = $Folder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()

        #delete all subfolders
        Foreach($Folder in $SubFolders)
        {
            #Exclude "Forms" and Hidden folders

                #Call the function recursively to empty the folder
                Empty-SPOFolder -Folder $Folder -Force -Recurse

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

        }
    }
    Catch {
    write-host -f Red "Error: $Folder.UniqueId - $File.Name " $_.Exception.Message
    }
}
Windows for business Windows Server User experience PowerShell
{count} votes

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.