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
To delete all documents in SharePoint library, use this PowerShell:
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?
To delete all files in SharePoint Online, refer: PowerShell to Bulk Delete Files in SharePoint Online
SharePoint PowerShell to Delete All Documents
To delete all documents in 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 "http://intranet.crescent.com/sales" $Library = $Web.Lists.TryGetList("Documents") #Call the function to Delete all files in the Library Delete-AllFilesFromLibrary $Library.RootFolderPlease 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 "http://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?
ReplyDelete