Previously I had a little console app to efficiently empty large SharePoint lists. Today I converted it to powershell.
Compared to the simple item by item delete with item.Delete() its 30 times faster; on my dev machine it deletes ~30 items per second. It works for SharePoint 2010 and should for 2013 (not tested, yet).
Script
param($weburl,$listname)
if ($weburl -eq $null -or $listname -eq $null)
{
write-host -foregroundcolor red "-weburl or -listname are null."
return
}
Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
$web = get-spweb $weburl
$list = $web.lists[$listname]
$stringbuilder = new-object System.Text.StringBuilder
try
{
$stringbuilder.Append("") > $null
$i=0
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewFieldsOnly = $true
$items = $list.GetItems($spQuery);
$count = $items.Count
while ($i -le ($count-1))
{
write-host $i
$item = $items[$i]
$stringbuilder.AppendFormat("", $i) > $null
$stringbuilder.AppendFormat("{0}", $list.ID) > $null
$stringbuilder.AppendFormat("{0}", $item.Id) > $null
$stringbuilder.Append("Delete") > $null
$stringbuilder.Append("") > $null
$i++
}
$stringbuilder.Append("") > $null
$web.ProcessBatchData($stringbuilder.ToString()) > $null
}
catch
{
Write-Host -ForegroundColor Red $_.Exception.ToString()
}
write-host -ForegroundColor Green "done."
Usage
Delete-Items.ps1 -weburl [url of web] -listname [name of list]
Further improvements
I think if the list is really huge (100k items or greater) the items should be deleted in batches.
I have not tried it with a document library, maybe there are issues. If there are issues, please leave a comment!
Disclaimer
There is no way back - if you start the script there is no “ARE YOU SURE?” - all data is gone in a very short time.
Share this post
Twitter
Facebook
LinkedIn
Email