OK, so these days one of my site’s visitors (thanks Alice) mentioned that my previous script to delete document library folder contents using PNP from [ here ] is only deleting files and folders cannot be deleted anymore when using PnP.PowerShell 1.10.0.
The exception when trying to remove a folder with the script is: Remove-PnPFolder : Server relative urls must start with SPWeb.ServerRelativeUrl
True that the script worked with PnP.PowerShell 1.9.0.
That being said, I rebuilt a new script:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/teams/testpalma" $FolderURL = "mydoc1/folder1" Try { #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive $ctx=Get-PnPContext $web=$ctx.web #sharepoint online powershell delete folder $Folder=$web.GetFolderByServerRelativeUrl($FolderURL) #sharepoint online powershell delete folder in document library $Folder.DeleteObject() $Ctx.ExecuteQuery() Write-host "Folder deleted Successfully!" -ForegroundColor Green } catch { write-host "Error: $($_.Exception.Message)" -foregroundcolor Red } |
Note: the above script completely deletes the document library folder.
In case you want to “move” the folder to the recycle bin instead, use the following script:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/teams/testpalma" $FolderURL = "mydoc1/folderA" Try { #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive $ctx=Get-PnPContext $web=$ctx.web #sharepoint online powershell recycle folder $Folder=$web.GetFolderByServerRelativeUrl($FolderURL) #sharepoint online powershell recyble bin folder in document library $Folder.Recycle() $Ctx.ExecuteQuery() Write-host "Folder deleted Successfully!" -ForegroundColor Green } catch { write-host "Error: $($_.Exception.Message)" -foregroundcolor Red } |
Enjoy!
