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:
  • 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. 
    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 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 $FileRelativeURL
This 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
To Delete all Files from a SharePoint Online document library, use: SharePoint Online: Delete All Files from a Document Library using PowerShell

Delete File in SharePoint Online using PnP 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!
SharePoint Online: How to Delete a File from Document Library using PowerShell? SharePoint Online: How to Delete a File from Document Library using PowerShell? Reviewed by Salaudeen Rajack on February 25, 2017 Rating: 5

1 comment:

  1. How can we delete files and folders based on modified date.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.