SharePoint Online: Delete All Files in a Document Library using PowerShell
Requirement: Delete all documents in library in SharePoint Online
How to Delete All Documents in Library in SharePoint Online?
At times you may want to delete multiple documents all at once. You can delete multiple files, folders and sub-folders from a SharePoint Online document library in few clicks:
SharePoint Online: Delete All Files in Document Library using PowerShell
This PowerShell script deletes all files from a given library - without deleting any folders in it!
PowerShell to Delete All Files and Folders in SharePoint Online Document Library
If you want to delete all files and sub-folders in a library, use:
PnP PowerShell to Delete All Files in SharePoint Online Document Library
How do I bulk delete files in SharePoint Online? Well, This PowerShell script deletes all files from all folders and sub-folders of the given document library. (doesn't deletes the folders, BTW!)
Empty a SharePoint Online Document Library using PnP PowerShell
How about emptying a document library by deleting all files, folders and sub-folders from it?
How to Delete All Documents in Library in SharePoint Online?
At times you may want to delete multiple documents all at once. You can delete multiple files, folders and sub-folders from a SharePoint Online document library in few clicks:
- Navigate to your SharePoint Online document library in browser, hover over header and click the check mark to select all files.
- Click on "Delete" button on the top link bar.
- In the Delete dialog box, confirm the delete by clicking "Delete" button
- You'll find the little status box appear in the upper top of the document library telling you that the item has been deleted.
Tips: You can also use Explorer view to delete multiple files! Go to the library >> Click on "Open with Explorer" option from the Library's ribbon tab. Select and delete Files!
SharePoint Online: Delete All Files in Document Library using PowerShell
This PowerShell script deletes all files from a given library - without deleting any folders in it!
#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 to Delete all files from a given Folder Function Delete-AllFilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder) { Try { #Get All Files from the Folder $Ctx = $Folder.Context $Files = $Folder.Files $Ctx.Load($Files) $Ctx.ExecuteQuery() #Iterate through each File in the Root folder Foreach($File in $Files) { #Delete the file $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'" } $Ctx.ExecuteQuery() #Process all Sub Folders of the given folder $SubFolders = $Folder.Folders $Ctx.Load($SubFolders) $Ctx.ExecuteQuery() Foreach($Folder in $SubFolders) { #Exclude "Forms" and Hidden folders If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_")))) { #Call the function recursively Delete-AllFilesFromFolder -Folder $Folder } } } Catch { write-host -f Red "Error:" $_.Exception.Message } } #Variables for Processing $SiteURL = "https://crescenttech.sharepoint.com/sites/Marketing" $LibraryRelativeURL = "/sites/Marketing/Shared Documents" #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 Root Folder of the Library $RootFolder=$Ctx.Web.GetFolderByServerRelativeUrl($LibraryRelativeURL) $Ctx.Load($RootFolder) #Call the function to delete all Files from a Folder Delete-AllFilesFromFolder -Folder $RootFolderPlease note, These scripts uses "Recycle" method to send files to recycle bin. You can use "Delete" method to permanently delete files!
PowerShell to Delete All Files and Folders in SharePoint Online Document Library
If you want to delete all files and sub-folders in a library, use:
#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" #Variables for Processing $SiteURL = "https://crescenttech.sharepoint.com/sites/marketing" $LibraryRelativeURL = "/Sites/Marketing/Shared Documents" 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 Library and Files $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($LibraryRelativeURL) $Ctx.Load($Folder) $Files = $Folder.Files $Ctx.Load($Files) $Ctx.ExecuteQuery() #Iterate through each File in the Root folder Foreach($File in $Files) { #Delete the file $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'" } $Ctx.ExecuteQuery() #Process all Sub Folders $SubFolders = $Folder.Folders $Ctx.Load($SubFolders) $Ctx.ExecuteQuery() Foreach($SubFolder in $SubFolders) { #Exclude "Forms" and Hidden folders If( ($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_")))) { #Delete the folder $Folder.Folders.GetByUrl($SubFolder.ServerRelativeUrl).Recycle()| Out-Null Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'" } $Ctx.ExecuteQuery() } } Catch { write-host -f Red "Error:" $_.Exception.Message }If you want to delete a particular file, use: Delete Files in SharePoint Online using PowerShell
PnP PowerShell to Delete All Files in SharePoint Online Document Library
How do I bulk delete files in SharePoint Online? Well, This PowerShell script deletes all files from all folders and sub-folders of the given document library. (doesn't deletes the folders, BTW!)
#Parameters $SiteURL = "https://crescent.sharepoint.com/sites/icdocs" $ListName = "Images" #Connect to the Site Connect-PnPOnline -URL $SiteURL -UseWebLogin #Get the web & document Library $Web = Get-PnPWeb $List = Get-PnPList -Identity $ListName #Function to delete all items in a folder - and sub-folders recursively Function Delete-AllFilesFromFolder($Folder) { #Get the site relative path of the Folder If($Folder.Context.web.ServerRelativeURL -eq "/") { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl } Else { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.web.ServerRelativeURL,"") } #Get All files in the folder $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File #Delete all files ForEach ($File in $Files) { Write-Host ("Deleting File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL) #Delete Item Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle } #Process all Sub-Folders $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder Foreach($Folder in $SubFolders) { #Exclude "Forms" and Hidden folders If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_")))) { #Call the function recursively Delete-AllFilesFromFolder -Folder $Folder } } } #Get the Root Folder of the Document Library and call the function Delete-AllFilesFromFolder -Folder $List.RootFolderWe can also use Move-PnPListItemToRecycleBin cmdlet to delete files and send them to the recycle bin.
#Config Variables $SiteURL = "https://crescent.sharepoint.com/sites/marketing" $LibraryName = "Project Documents" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) #Get All Files from the document library - Exclude Folder Objects $Files = Get-PnPListItem -List $LibraryName -PageSize 1000 | Where {$_["FileLeafRef"] -like "*.*"} #Loop through each File Write-host -f Green "Number of Files Found:"$Files.Count ForEach($File in $Files) { Write-Host ("Deleting File '{0}' at {1}" -f $File["FileLeafRef"], $File["FileRef"]) #Send File Move-PnPListItemToRecycleBin -List $LibraryName -Identity $File.Id -Force }
Empty a SharePoint Online Document Library using PnP PowerShell
How about emptying a document library by deleting all files, folders and sub-folders from it?
#Parameters $SiteURL = "https://crescent.sharepoint.com/sites/marketing" $LibraryName = "Documents" #Connect to the Site Connect-PnPOnline -URL $SiteURL -UseWebLogin #Get the web & Root folder of the library $Web = Get-PnPWeb $Library = Get-PnPList -Identity $LibraryName -Includes RootFolder $RootFolder = $Library.RootFolder #Function to delete all Files and sub-folders from a Folder Function Empty-PnPFolder($Folder) { #Get the site relative path of the Folder If($Folder.Context.web.ServerRelativeURL -eq "/") { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl } Else { $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.web.ServerRelativeURL,"") } #Delete all files in the Folder $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File ForEach ($File in $Files) { #Delete File Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle Write-Host -f Green ("Deleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL) } #Process all Sub-Folders $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder Foreach($SubFolder in $SubFolders) { #Exclude "Forms" and Hidden folders If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_")))) { #Call the function recursively Empty-PnPFolder -Folder $SubFolder #Delete the folder Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force -Recycle Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL) } } } #Call the function to empty document library Empty-PnPFolder -Folder $RootFolder
Thank you , this is so usefull.
ReplyDeleteDo you have any suggesstion for if we have a csv file with full link to file need to delete and powershell can import them / auto spit link and fill them to $SiteURL and $LibraryRelativeURL tp process the deletion ?
Appriciated.
Sure, use this script: SharePoint Online: Bulk Delete Files from a CSV using PowerShell
Delete