Back to Insights

Delete a whole lot of files from a SharePoint site with PowerShell

Here’s a little PowerShell script that you can use to delete all the files in every document library in a site:

$web = Get-SPWeb "http://extranet.example.com/sites/SuperImportantStuff/Secrets"
$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}

foreach ($library in $Libraries) {

	Write-Output "Getting files from $($library.Title)"
	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
	foreach ($file in $Files) {
		Write-Output "Deleting file $($file.Name)..."
		$file.Delete()
	}
}

To be honest, you’re likely to never use this. I mean, for what purpose would you need to delete every file in every document library in a site? To be fair, this post was inspired by a real question on the MSDN Forums: How to remove specific file types (i.e. docx) from multiple specific Sharepoint libaries using Powershell? In this case the desire was to remove a specific file type from a group of document libraries (and if you follow the link you can see the script I wrote to do just that).

So maybe deleting every file in the site is a bit of a stretch, but the algorithm itself is useful. We’re looping through every file in every document library. While not necessarily efficient, it’s thorough and you could use this as a framework for other sweeping changes or reporting.

For example you can filter based on file name, and delete only Word documents by adding another filter to line 7:

	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.docx" -Or $_.File -Like "*.docm" -Or $_.File -Like "*.doc")}

Maybe you only want to target a specific library. To do this, modify line 2:

$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -eq "Secret Documents"}

Now no one will know your secrets!

Maybe you don't want to delete the files but instead display the size of the file (in bytes) because you want to keep on top of the growth in your site using in this example a not-so-easy-to-read report. For this, you’ll want to replace lines 9 and 10 with something like:

		Write-Output "$($file.Name), $($file.Length)"

Finally, say you actually do have a need to delete every document, but on a grand scale. How about if you delete every document in every document library in every site in the entire site collection? Obviously you’re drunk with administrative power but since you’ve asked, here it is:

$site = Get-SPSite "http://extranet.example.com/sites/SuperImportantStuff"
foreach ($web in $site.AllWebs) {
	
	$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}

	foreach ($library in $Libraries) {

		Write-Output "Getting files from $($library.Title)"
		$Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
		foreach ($file in $Files) {
			Write-Output "Deleting file $($file.Name)..."
			$file.Delete()
		
		} # foreach file
	
	} # foreach library
	
} # foreach web

Way to go, now all your super important stuff is in the recycle bin.

Full code listings

Here are the full listings for the three examples so it’s clear what I meant when I described changing lines.

Delete all Word documents

$web = Get-SPWeb "http://extranet.example.com/sites/SuperImportantStuff/Secrets"
$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}

foreach ($library in $Libraries) {

	Write-Output "Getting files from $($library.Title)"
		$Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.docx" -Or $_.File -Like "*.docm" -Or $_.File -Like "*.doc")}
	foreach ($file in $Files) {
		Write-Output "Deleting file $($file.Name)..."
		$file.Delete()
	}
}

Delete the Secret Documents documents

$web = Get-SPWeb "http://extranet.example.com/sites/SuperImportantStuff/Secrets"
$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary" -And $_.Title -eq "Secret Documents"}

foreach ($library in $Libraries) {

	Write-Output "Getting files from $($library.Title)"
	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
	foreach ($file in $Files) {
		Write-Output "Deleting file $($file.Name)..."
		$file.Delete()
	}
}

Show file sizes (in bytes)

$web = Get-SPWeb "http://extranet.example.com/sites/SuperImportantStuff/Secrets"
$Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}

foreach ($library in $Libraries) {

	Write-Output "Getting files from $($library.Title)"
	$Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
	foreach ($file in $Files) {
		Write-Output "$($file.Name), $($file.Length)"
	}
}

Disclaimer

Here’s the part where I suggest you think before you act and make sure you mean to delete these files before you run these scripts.

Test them in an environment where you don't mind breaking things. Take a backup before running this for real. Comment out the $file.Delete() to be sure it's going to delete what you want. You are your own person and are responsible for your own actions. Otherwise, have fun.

Share