Requirement: Empty a Folder in SharePoint Online document library by deleting all its files and Sub-Folders recursively.

PowerShell to Delete All Files and Sub-Folders from a SharePoint Online Folder
```#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#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.ExecuteQuery()

#Iterate through each File in the Root folder
Foreach(\$File in \$Files)
{
#Delete the file
\$Folder.Files.GetByUrl(\$File.ServerRelativeUrl).Recycle() | 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.ExecuteQuery()
Foreach(\$Folder in \$SubFolders)
{
#Exclude "Forms" and Hidden folders
If( (\$Folder.Name -ne "Forms") -and (-Not(\$Folder.Name.StartsWith("_"))))
{
#Call the function recursively to empty the folder
Empty-SPOFolder -Folder \$Folder

#Delete the folder
\$Ctx.Web.GetFolderById(\$Folder.UniqueId).Recycle() | Out-Null
\$Ctx.ExecuteQuery()
Write-host  -f Green "Deleted Folder:"\$Folder.ServerRelativeUrl
}
}
}
Catch {
write-host -f Red "Error:" \$_.Exception.Message
}
}

#Variables
\$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"
\$ServerRelativeUrl= "/Sites/Marketing/Project Documents/2018"

Try {
#Get Credentials to connect
\$Cred= Get-Credential

#Setup the context
\$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext(\$SiteURL)

#Get the web from URL
\$Web = \$Ctx.web
\$Ctx.executeQuery()

#Get the Folder object by Server Relative URL
\$Folder = \$Web.GetFolderByServerRelativeUrl(\$ServerRelativeUrl)
\$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
} ```
This PowerShell script deletes all files, sub-folders of a folder and then deletes the given folder too.
SharePoint Online: Delete All Files and Sub-Folders from a Folder Recursively using PowerShell Reviewed by Salaudeen Rajack on August 21, 2018 Rating: 5
{"extraction_info": {"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9111166596412659, "perplexity": 21400.48069348109}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-26/segments/1560628001014.85/warc/CC-MAIN-20190627075525-20190627101525-00137.warc.gz"}