SharePoint Online: Bulk Delete Files from a CSV using PowerShell

Requirement: Delete multiple files from SharePoint Online document libraries from a CSV using PowerShell

How to Delete Multiple Files in SharePoint Online Site from a CSV file?

PowerShell can be your powerful ally when it comes to managing your files and folders in Sharepoint Online. I was looking for a way to delete multiple files from a Sharepoint Online site quickly but didn’t want to manually delete files one by one through the web browser. In this blog post, Let me share how to use PowerShell to delete multiple files in Sharepoint Online through a CSV file.

We have a CSV file with a list of URLs of various files in multiple document libraries in SharePoint Online. I wanted to bulk delete them. Here is my CSV file format: It has “FileName” and “ServerRelativeURL” as columns.

powershell to bulk delete files in sharepoint online

You can download the CSV template at:

PowerShell to Remove Files using a CSV file in SharePoint Online:

Here is the PowerShell script that deletes each file from the CSV file in a given site:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFilePath = "C:\Temp\BulkDeleteFiles.csv"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Read from CSV file and delete
    Import-CSV $CSVFilePath | ForEach-Object {
        #Check if File exists
        $File = Get-PnPFile -ServerRelativeUrl $_.ServerRelativeUrl -ErrorAction SilentlyContinue 
        If($File)
        {
            #Delete the File - Send to Recycle bin without prompting
            Remove-PnPFile -ServerRelativeUrl $_.ServerRelativeURL -Recycle -Force
            Write-Host "Deleted File '$($_.FileName)' at $($_.ServerRelativeUrl)" -ForegroundColor Green
        }
        Else
        {
            Write-Host "File '$($_.FileName)' doesn't exists at $($_.ServerRelativeUrl)" -ForegroundColor Yellow
        }
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}  

This can be useful if you need to quickly remove a large number of files that are no longer required on your site.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “SharePoint Online: Bulk Delete Files from a CSV using PowerShell

  • Thank you for putting this article together. We have a large number of files that were migrated from an on-prem server to SP. A number of these files have ~$ as the file name prefix that we would like to remove. When running the script, I receive notification that the file name doesn’t exist at the specified path. However, when I check the site manually, I can confirm the file is present.

    Is there anything special about hidden/owner files that would cause this script to not pick up the file?

    Thank you!

    Reply
  • Is there a way to bulk delete files in different subsite and different site collection from one CSV?

    Reply
  • I’m getting the following error when I run this script:

    Error: Cannot bind argument to parameter ‘Url’ because it is null.

    Here is the script:

    #Parameters
    $SiteURL = "https://sapem.sharepoint.com/sites/Sandbox"
    $CSVFilePath = "C:\Temptest.csv"
    
    #Get Credentials to connect
    $Cred = Get-Credential
     
    Try {
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteURL -Credentials $Cred
     
        #Read from CSV file and delete
        Import-CSV $CSVFilePath | ForEach-Object {
            #Check if File exists
            $File = Get-PnPFile -ServerRelativeUrl $_.ServerRelativeUrl -ErrorAction SilentlyContinue 
            If($File)
            {
                #Delete the File - Send to Recycle bin without prompting
                Remove-PnPFile -ServerRelativeUrl $_.ServerRelativeURL -Recycle -Force
                Write-Host "Deleted File '$($_.FileName)' at $($_.ServerRelativeUrl)" -ForegroundColor Green
            }
            Else
            {
                Write-Host "File '$($_.FileName)' doesn't exists at $($_.ServerRelativeUrl)" -ForegroundColor Yellow
            }
        }
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
    

    Any reason why this could be happening?

    Reply
    • The CSV file must have the “ServerRelativeURL” column with the value of ServerRelativeURL of the file to be deleted!

      Reply
      • How do I delete in batches, I am trying to use this code in a large list with 40k records? This is exactly what I need but ps is freezing due to the size of the library.

        Reply

Leave a Reply

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