Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

4 Answers 4

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

share|improve this answer

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 {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        # Delete file by deleting parent SPListItem
        Write-Host("DELETED FILE: " + $file.name)
        $list.Items.DeleteItemById($file.Item.Id)
    }
}

# Delete root files
DeleteFiles($list.RootFolder.Url)

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

# Delete folders
foreach ($folder in $list.Folders) {
    try {
        Write-Host("DELETED FOLDER: " + $folder.name)
        $list.Folders.DeleteItemById($folder.ID)
    }
    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

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 );
   4:     
   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}" );
   8:     
   9:     foreach ($item in $spList_list.Items)
  10:     {
  11:         if($item -ne $null)
  12:             {$deleteBldr.Append([System.String]::Format($command, $item.ID.ToString()));}
  13:     }
  14:     
  15:     $deleteBldr.Append("</Batch>");
  16:     
  17:     $spWeb.ProcessBatchData($deleteBldr.ToString());
  18: }
share|improve this answer

Your Answer

 
discard

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.