How to delete large OneDrive folder and nested subfolders

Copper Contributor

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's executing.

 

```
#Variables
$SiteURL = "OneDrive URL"
$ServerRelativeUrl= "/Zipped_Documents"



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).Recycle() | Out-Null
$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

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

```

 

 

 

 

```

3 Replies

HI

Are you getting any error message that you can share.

and how big is the file and last question, how many folder are nested inside each other.

 

Hi..I'm deleting it from OneDrive folder that has like over 4million item and about 900GB. and this are the error that I get when a subfolder has a lot and lots of items inside. Also there are too many nested subfolder so I can't really count.

```
Error: Microsoft.SharePoint.Client.Folder.UniqueId - Microsoft.SharePoint.Client.File.Name Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out."
Error: Microsoft.SharePoint.Client.Folder.UniqueId - .Name Exception calling "ExecuteQuery" with "0" argument(s): "The attempted operation is prohibited because it exceeds the list view threshold."
```

@aasenomad 

This is a lot of data and requests sent in one patch to the server, and I don't think the servers will be happy to serve your request.

I guess the failure is on the delete part Empty-SPOFolder, not on reading the content. 

I guess I read a similar threat a long time ago, and the answer was to use the latest version of the SharePoint management shell.