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?
Have you ever had to bulk delete multiple folders from SharePoint Online to clean up your site or remove outdated or unwanted content? If so, you’re probably familiar with the challenge of selecting each folder and then deleting it. This blog post will show you how to quickly delete multiple folders in SharePoint Online.
We can use a CSV file with a PowerShell script to bulk delete folders with their subfolders and files:
#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
$CSVFilePath = "C:\Users\Thomas\Desktop\BulkDeleteFolders.csv"
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#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
}
}
This script allows you to delete multiple folders at once, making it a more efficient process. This can be a helpful technique if you need to remove a large number of folders from your site quickly. Here is my CSV file with a list of folder names and the site relative URL of its parent folder.
You can download the CSV to delete folders from here:
SharePoint Online: Can’t Delete Folder with Files?
The above script 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 the “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,[string]::Empty)
}
#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 -Interactive
#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 and sub-folders first and then deletes the 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!
Sure! Here you go: SharePoint Online: Bulk Delete Files from a CSV using PowerShell