PowerShell to Delete All Files in SharePoint Document Library
Requirement: Delete All Files in SharePoint Document Library using PowerShell.
How to Delete All Files in a SharePoint Document Library?
To delete all files in a SharePoint document library, follow these steps:
- Open the document library in SharePoint that you want to delete all files from.
- Click on the checkbox in the header of the leftmost column to select all files in the library. If there are multiple pages of files in the library, you may need to repeat this step on each page to select all files.
- Click on the “Delete” button in the toolbar at the top of the page.
- Confirm that you want to delete all selected files by clicking “OK” on the confirmation prompt.
This deletes all selected files and folders from the document library, and they will be sent to the recycle bin.
SharePoint PowerShell to Delete All Documents
If you want to clear out all the files from a SharePoint document library, PowerShell provides an easy way to do it. This article will show you how to use PowerShell to delete all files in a SharePoint document library.
To delete all documents in a SharePoint library, use this PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Delete all files in a Folder
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
#Delete All Files in the Folder
Foreach ($File in @($Folder.Files))
{
#Delete the file
$File.Recycle() | Out-Null
Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
}
#Delete files in Sub-folders
Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
{
#Call the function recursively
Delete-AllFilesFromLibrary($SubFolder)
}
}
#Get the Web and Library
$Web = Get-SPWeb "https://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")
#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder
Please note, this script sends all files from all folders-sub folders in a library to recycle bin. If you want to permanently delete files use: $File.Delete() method instead of $File.Recycle().
PowerShell to Delete All Items from SharePoint Library
The above script deletes all files – without deleting folders and sub-folders. What if you want to remove all files and folders?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Delete all files in a Folder
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
#Delete All Files in the Folder
Foreach ($File in @($Folder.Files))
{
#Delete the file
$File.Delete() | Out-Null
Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
}
#Delete files in Sub-folders
Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
{
#Call the function recursively
Delete-AllFilesFromLibrary($SubFolder)
}
#Delete folders
ForEach ($SubFolder in @($Folder.SubFolders))
{
#Exclude "Forms" and Hidden folders
If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
{
#Delete the Sub-Folder
$SubFolder.Delete() | Out-Null
Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'"
}
}
}
#Get the Web and Library
$Web = Get-SPWeb "https://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")
#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder
To delete all files in SharePoint Online, refer: PowerShell to Bulk Delete Files in SharePoint Online
Thank you so much for this site. but I want to do a one time permanent deletion $File.Delete() of all items older than 14 months. Any suggestions?