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?
Deleting files is one of the most common tasks you will need to do with SharePoint. 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.
How do I remove a file from SharePoint Online? To delete a single document from a library, do the following:
- Navigate to your SharePoint Online site and then the document library through the web browser.Â
- 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.
- You can also hover over the document you want to delete and click “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 SharePoint 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
If you’re looking for a quick and easy way to delete files from SharePoint Online, PowerShell is the Answer. Let me show you how to use PnP PowerShell to delete a file from SharePoint Online.
Let’s delete a file in SharePoint Online PowerShell using the Remove-PnPFile cmdlet. To use this cmdlet, you first need to connect to your SharePoint Online site using the Connect-PnPOnline cmdlet. Once connected, you can then use the Remove-PnPFile cmdlet to delete the file. The Remove-PnPFile cmdlet takes the server-relative URL or the Site-relative URL of the file to be deleted as its parameter. The -force parameter is optional, but if you include it, it will delete the file without prompting for confirmation.
#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 the -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
To delete a SharePoint folder, navigate to the folder or subfolder you want to delete, select the folder, and click on the “Delete” button on the toolbar. You will be prompted to confirm the deletion, and once confirmed, the folder and all its contents will be deleted.
More info: How to delete a folder in SharePoint Online?
SharePoint Online’s built-in Recycle Bin retains deleted files for 93 days. To recover deleted files, go to Site Settings >> Site Contents. Click on Recycle Bin (or use the “Recycle bin” link under Site Collection Administration). Select the files you want to restore with the check box next to the folders, and click Restore. The selected files will be restored to their original location. If the files are not in the Recycle Bin, you can try using the second stage recycle bin link at the bottom of the page. SharePoint Online PowerShell cmdlets can also be used to restore deleted files.
More info: How to Restore Deleted Files in SharePoint Online?
When a file is deleted in SharePoint, it is moved to the Recycle Bin. The file will remain in the Recycle Bin for a certain period of time (usually 93 days) before it is permanently deleted. During this time, the file can be restored if needed. If the file is deleted from both the 1st stage and 2nd stage Recycle Bins, it cannot be recovered and is permanently lost.
To delete a SharePoint document library, navigate to the library you want to delete, click on the gear icon in the top-right corner, select “Library settings,” and then click on “Delete this document library” under the “Permissions and Management” section. Confirm the deletion, and the library will be removed from the site. The same procedure applies to the SharePoint list as well.
More info: Delete a document library in SharePoint Online
To permanently delete a file from SharePoint, you must first delete it from both stages of the recycle bin. Go to the site settings and select “Recycle Bin” under “Site Collection Administration”. Select the File and click “Delete” to remove it from the 1st stage recycle bin. And then click on the “Second-stage” recycle bin link from the bottom of the page and delete the file from there too!
I tried to delete a large file and getting the timed out error. Any advice? Thank you in advance!
Error deleting file ! Exception calling “ExecuteQuery” with “0” argument(s): “The operation has timed out”
I want the same in On prem?
Here is the PowerShell script to delete a file from SharePoint On-Premises: How to Delete a File in SharePoint using PowerShell?
How can we delete files and folders based on modified date.
Use this Script: SharePoint Online: Delete Files Older than 30 days using PowerShell