PowerShell to Delete All Files in SharePoint Document Library
Requirement: Delete All Files in SharePoint Document Library using PowerShell
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?