SharePoint Online: How to Delete a File from Document Library using PowerShell?

Requirement:  Delete a file from the SharePoint Online document library using PowerShell

How to Delete a File in SharePoint Online?

One of the most common tasks you will need to do with SharePoint is to delete files. This blog post will show you how to delete a file in SharePoint Online. Deleting a file is a simple process and can be done in just a few steps. We’ll walk you through the process so that you can delete files quickly and easily. We’ll also see how to delete a file in SharePoint Online using PowerShell.

To delete a single document from a library, do the following:

  • Browse to the SharePoint Online site and then navigate to the document library. 
  • Select the File you want to delete and click on the “Delete” button from the toolbar. You can also select and delete multiple files!
  • Confirm the deletion by clicking on the “Delete” button in the confirmation popup. 
    sharepoint online delete file powershell
  • You can also hover over the document you want to delete and click on “Delete” from the context menu. You’ll find a notification at the top of the document library telling you that the item has been deleted. The deleted item is moved to the site’s recycle bin.

SharePoint Online: PowerShell to Delete a File from Document Library

If you’re like me, you probably have a lot of files in your SharePoint Online site that you no longer need. Deleting these files one by one can be really tedious, but with PowerShell, it’s easy! This blog post will show you how to use PowerShell to delete files from your SharePoint Online site. Let’s get started!

Here is how to delete a SharePoint Online file using PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-SPOFile()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FileRelativeURL
    )
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the file to delete
        $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()
                
        #Delete the file
        $File.DeleteObject()
        $Ctx.ExecuteQuery()

        write-host -f Green "File has been deleted successfully!"
     }
    Catch {
        write-host -f Red "Error deleting file !" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$FileRelativeURL="/sites/Ops/Shared Documents/Investment Process.pptx"

#Call the function 
Remove-SPOFile -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL

This script deletes the file from the given URL. There are scenarios where you may have to delete a file using its item ID. Here is another script to remove a document using its item ID in SharePoint Online.

Delete a File from SharePoint Online Library using PowerShell:

Let’s delete a document from SharePoint Online using its Item ID this time.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-SPOFile()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $LibraryName,
        [Parameter(Mandatory=$true)] [string] $ItemID
    )
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the list by title
        $List=$Ctx.web.Lists.GetByTitle($LibraryName)
        
        #Get the item to delete by ID
        $Item=$List.GetItemById($ItemID)
        #Get the file to delete
        $File=$Item.File

        #Delete the file
        $File.DeleteObject()
        $Ctx.ExecuteQuery()

        write-host -f Green "File has been deleted successfully!"
     }
    Catch {
        write-host -f Red "Error deleting file !" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$LibraryName="Documents"
$ItemID="16"

#Call the function 
Remove-SPOFile -SiteURL $SiteURL -LibraryName $LibraryName -ItemID $ItemID

Delete File in SharePoint Online using PnP PowerShell

Let’s delete a file in SharePoint Online PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$FileRelativeURL ="/sites/Marketing/Shared Documents/Possible Deals.xlsx"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Try to Get File
    $File = Get-PnPFile -Url $FileRelativeURL -ErrorAction SilentlyContinue
    If($File)
    {
        #Delete the File
        Remove-PnPFile -ServerRelativeUrl $FileRelativeURL -Force
        Write-Host -f Green "File $FileRelativeURL deleted successfully!"
    }
    Else
    {
        Write-Host -f Yellow "Could not Find File at $FileRelativeURL"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This removes the File permanently. Use -Recycle switch to send the file to recycle bin instead!

To delete all files from a SharePoint Online document library, use: SharePoint Online: Delete All Files from a Document Library using PowerShell

Unable to delete items in SharePoint or OneDrive?

There are some root causes, why you are not able to delete a file or folder in SharePoint Online:

  • You may not have the necessary permissions to delete!
  • Files may be checked out to other users – You got to check in first and then delete
  • There could be a Retention policy applied – that blocks deletion. E.g., To delete a folder, you must delete all contents of the folder first.
  • Your site may be locked or reached the maximum storage limit

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: How to Delete a File from Document Library using PowerShell?

Leave a Reply

Your email address will not be published.