SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone tell me the quickest way to delete a large number of documents from a site library if you are an administrator for the site? Any powershell commands?

share|improve this question

Here is a PowerShell script to delete all the files and folders from a document library:

Add-PSSnapin Microsoft.SharePoint.PowerShell

$web = Get-SPWeb -Identity "http://sp2010srv"
$list = $web.GetList("http://sp2010srv/DocLib")

function DeleteFiles {
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        # Delete file by deleting parent SPListItem
        Write-Host("DELETED FILE: " + $

# Delete root files

# Delete files in folders
foreach ($folder in $list.Folders) {

# Delete folders
foreach ($folder in $list.Folders) {
    try {
        Write-Host("DELETED FOLDER: " + $
    catch {
        # Deletion of parent folder already deleted this folder

I can't take full credit for this code; it was heavily adapted from Vedran Rasol's answer to this question.

share|improve this answer

I always end up using "Open in Windows Explorer", as that's quicker for me than trying to script something.

share|improve this answer

If speed of deletion is critical for you (you have really large amount of data), use batch operations. This post could be helpful for you, you can easily adopt it to the PowerShell Script. Batch operations are more fast, I think few times faster.

share|improve this answer

Use the list function ProcessBatchData(). Basically you create an XML with all the items to delete and pass this XML to this function. It's faster than iterating over all the items and delete them one by one

   1: function DeleteAllSPListItems
   2: {
   3:     param (    [Microsoft.SharePoint.SPList] $spList_list );
   5:     [System.Text.StringBuilder]$deleteBldr = New-Object "System.Text.StringBuilder";
   6:     $deleteBldr.Append("<?xml version=""1.0"" encoding=""UTF-8""?><Batch>");
   7:     $command = [System.String]::Format( "<Method><SetList Scope=""Request"">{0}</SetList><SetVar Name=""ID"">{1}</SetVar><SetVar Name=""Cmd"">Delete</SetVar></Method>", $spList_list.ID, "{0}" );
   9:     foreach ($item in $spList_list.Items)
  10:     {
  11:         if($item -ne $null)
  12:             {$deleteBldr.Append([System.String]::Format($command, $item.ID.ToString()));}
  13:     }
  15:     $deleteBldr.Append("</Batch>");
  17:     $spWeb.ProcessBatchData($deleteBldr.ToString());
  18: }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.