SharePoint Online: How to Delete a File from Document Library using PowerShell?
Requirement: Delete a file from SharePoint Online document library using PowerShell
How to Delete a File in SharePoint Online?
To delete a single document from a library, do the following:
SharePoint Online: PowerShell to Delete a File from Document Library
Here is how to delete a SharePoint Online file using PowerShell
Delete a File from SharePoint Online Library using PowerShell:
This time, lets delete a document from SharePoint Online using its Item id.
Delete File in SharePoint Online using PnP PowerShell
Let's delete a file in SharePoint Online PowerShell
To delete all files from a SharePoint Online document library, use: SharePoint Online: Delete All Files from a Document Library using PowerShell
How to Delete a File in SharePoint Online?
To delete a single document from a library, do the following:
- In SharePoint Online site, Navigate to the document library.
- Select the File you want to delete and Click on "Delete" button from the toolbar. You can also select and delete multiple files!
- Confirm delete by clicking on "Delete" button in the confirmation popup.
- You can also hover over the document you want to delete and click on "Delete" from the context menu. You'll find a notification in 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
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 $FileRelativeURLThis script deletes the file from given URL. There are scenarios where you may have to delete a file using its item id. Here is the another script to remove a document using its item ID in SharePoint Online.
Delete a File from SharePoint Online Library using PowerShell:
This time, lets delete a document from SharePoint Online using its Item id.
#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://crescenttech.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
How can we delete files and folders based on modified date.
ReplyDeleteUse this Script: SharePoint Online: Delete Files Older than 30 days using PowerShell
DeleteI want the same in On prem?
ReplyDeleteHere is the PowerShell script to delete a file from SharePoint On-Premises: How to Delete a File in SharePoint using PowerShell?
Delete