SharePoint Online: Bulk Delete Folders from a CSV using PowerShell
Requirement: Bulk Delete Folders in SharePoint Online.
How to Bulk Delete Multiple Folders in SharePoint Online?
To bulk delete folders with its sub-folders and files, we can use a CSV file with PowerShell script.
#Set Parameters $SiteURL = "https://crescent.sharepoint.com/sites/Projects" $CSVFilePath = "C:\Users\Thomas\Desktop\BulkDeleteFolders.csv" #Connect to the site Connect-PnPOnline -Url $SiteURL -UseWebLogin #Get the data from CSV file $CSVFile = Import-CSV $CSVFilePath | Sort-Object ParentFolderSiteRelativeURL -Descending #Read CSV file and delete each folder ForEach($Row in $CSVFile) { Try { #Check if folder exists $Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $Row.ParentFolderSiteRelativeURL -ItemName $Row.FolderName #-ErrorAction SilentlyContinue If($Folder -ne $Null) { #Delete the folder Remove-PnPFolder -Name $Row.FolderName -Folder $Row.ParentFolderSiteRelativeURL -Recycle -Force Write-host -f Green "Folder '$($Row.FolderName)' Deleted Successfully from $($Row.ParentFolderSiteRelativeURL)" } Else { Write-Host "Folder '$($Row.FolderName)' doesn't exists at $($Row.ParentFolderSiteRelativeURL)" -ForegroundColor Yellow } } Catch { write-host -f Red "`tError:" $_.Exception.Message } }
Here is my CSV file with list of folder names and the site relative URL of its parent folder.
You can download the CSV to delete folders: CSV to Bulk Delete Folders in SharePoint Online
SharePoint Online: Can't Delete Folder with Files?
The above script absolutely works fine when your site is not placed under any retention hold policies. However you got to delete all files and sub-folders of the folder to avoid "You have to delete all the items in this folder before you can delete this folder." error message.
SharePoint Online: PowerShell script to Bulk Delete Folders from a CSV File
This PowerShell script recursively deletes each folder and its contents (files, sub-folders) from the CSV file in a given site.
#Set Parameters $SiteURL = "https://crescent.sharepoint.com/sites/Marketing" $CSVFilePath = "C:\Users\Thomas\Desktop\BulkDeleteFolders.csv" #Function to delete all Files and sub-folders from a Folder Function Empty-PnPFolder($Folder) { #Get the site relative path of the Folder If($Folder.Context.Web.ServerRelativeUrl -eq "/") { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl } Else { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.Web.ServerRelativeURL,"") } #Delete all files in the Folder $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File ForEach ($File in $Files) { #Delete File Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle Write-Host -f Green ("`tDeleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL) } #Process all Sub-Folders $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder Foreach($SubFolder in $SubFolders) { #Exclude "Forms" and Hidden folders If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_")))) { #Call the function recursively Empty-PnPFolder -Folder $SubFolder #Delete the folder Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force -Recycle Write-Host -f Green ("`tDeleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL) } } } #Connect to the site Connect-PnPOnline -Url $SiteURL -UseWebLogin #Get the data from CSV file $CSVFile = Import-CSV $CSVFilePath | Sort-Object ParentFolderSiteRelativeURL -Descending #Read CSV file and delete each folder ForEach($Row in $CSVFile) { Try { #Check if folder exists $Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $Row.ParentFolderSiteRelativeURL -ItemName $Row.FolderName #-ErrorAction SilentlyContinue If($Folder -ne $Null) { Write-Host -f Yellow "Deleting Folder '$($Row.FolderName)' from $($Row.ParentFolderSiteRelativeURL)..." #Empty the Folder Empty-PnPFolder $Folder #Delete the folder Remove-PnPFolder -Name $Row.FolderName -Folder $Row.ParentFolderSiteRelativeURL -Recycle -Force Write-host -f Green "`tFolder '$($Row.FolderName)' Deleted Successfully!" } Else { Write-Host "Folder '$($Row.FolderName)' doesn't exists at $($Row.ParentFolderSiteRelativeURL)" -ForegroundColor Yellow } } Catch { write-host -f Red "`tError:" $_.Exception.Message } }
This script deletes all files, sub-folders first and then deletes folder in SharePoint Online.
To bulk delete files from a CSV file, use: SharePoint Online: Bulk Delete Files from a CSV using PowerShell
Hello... This is very useful. i would like to ask if it is possible to delete ONLY the files not including the folders and based on modified date... Please. Thank you!
ReplyDeleteSure! Here you go: SharePoint Online: Bulk Delete Files from a CSV using PowerShell
Delete