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?

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:

This PowerShell script 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
}  

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • 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