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 wanted 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 delete multiple folders in SharePoint Online quickly.

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 lets you delete multiple folders at once, making it more efficient. This can be a helpful technique if you need to quickly remove many folders from your site. Here is my CSV file with a list of folder names and the site-relative URL of its parent folder.

SharePoint Online Bulk Delete Folders

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

How do I delete a folder in SharePoint online?

To remove a folder in SharePoint Online, select the folder from the list or document library and click on the “delete” button from the toolbar. You can also right-click on the folder and choose “Delete” from the context menu. PowerShell too helps to delete folders in SharePoint Online.
More info: Delete a Folder in SharePoint Online PowerShell

How do I delete a file in SharePoint Online using PowerShell?

Use the $File.DeleteObject() method in CSOM or Remove-PnPFile cmdlet in PnP PowerShell to delete a file from SharePoint Online.
More info: SharePoint Online PowerShell to Delete File

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

7 thoughts on “SharePoint Online: Bulk Delete Folders from a CSV using PowerShell

  • Hi, this is a great script but i am getting the following error when deleting sub folders within a folder that sits inside the Document Library = Error: Server relative urls must start with SPWeb.ServerRelativeUrl

    Reply
  • Remove-PnPFolder : Server relative urls must start with SPWeb.ServerRelativeUrl

    it’s not working :/

    Reply
    • Use the “Folder” parameter value without leading “/”. E.g.
      Use “Shared Documents/Test”, instead of “/Shared Documents/Test”

      Reply
    • Can you ensure you have the latest PnP.PowerShell module installed and the urls are not starting with a “/”

      Reply
      • I had the same issue as TRutt and leniwa, managed to solve the problem by enclosing the ‘ParentFolderSiteRelativeURL’ values in the CSV file with single quotes, e.g. ‘Projects/Archive’. Hope this helps!

        Reply
  • 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!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *