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

Information Technology Professional with Two decades of SharePoint Experience.

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

  • June 12, 2021 at 8:55 AM

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

    Reply
  • November 10, 2020 at 9:37 AM

    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
    • November 10, 2020 at 1:24 PM

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

      Reply

Leave a Reply