EmptyList.ps1 – delete all SharePoint list items with PowerShell
Recently I needed to empty an InfoPath Form Library by deleting all of the items and skipping Recycle Bin. Datasheet view is great for small lists (a few hundred rows) but it doesn’t perform as well on large lists. Also, skipping the Recycle Bin is a unique requirement best handled with server side code. See the below PowerShell script for how to easily “empty” any SharePoint list or library. This works with both SharePoint 2010 and SharePoint 2007 (MOSS 2007). NOTE – Use this script carefully, it is really good at deleting. ![]()
# SharePoint On-Premise
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
$web = Get-SPWeb "http://sitename/"
$list = $web.Lists["Shared Documents"]
$caml=""
#optional filter
$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='Recursive'"
$query.Query=$caml
$items=$list.GetItems($query)
Write-Host $items.Count
$items | % { $list.GetItemById($_.Id).Delete() }
$web.Dispose()
$site.Dispose()
# SharePoint Online (SPO)
$webUrl = "https://tenant.sharepoint.com/sites/team"
$listTitle = "Shared Documents"
Connect-PnPOnline –Url $webUrl -UseWebLogin
$l = Get-PnPList $listTitle
$items = Get-PnPListItem -List $listTitle -PageSize 500
$batch = New-PnPBatch
foreach ($item in $items) {
$item.ID
Remove-PnPListItem -List $l -Identity $item.Id -Batch $batch
}
Invoke-PnPBatch -Batch $batch

This ran significantly faster than the script found here: http://saurabhmoss.wordpress.com/2011/03/11/sharepoint-2007-powershell-script-for-deleting-items-in-a-list
The other script was deleting 1 item from a 45,000-item list in 25 seconds; the one on this page deleted more than 20 items per second.
Thanks Bill! I’m glad you found it useful.
This is really Quick, I delete 300,000 in just few minutes
Thanks Ahmed! That’s a land speed record for this script. Glad to know it scales up well.
very cool. simple script that flies. Thanks Jeff!!
Thank you, you saved my time, its really working fast. Its very usefull.
How did you implent this code, really appreciate help with this 🙂
How and where do I add the script? What triggers the java scrip to run, would really appreciate help on this, if you could send an example, I want mine to delete from announcments list with a criteria on the title.
This is not working for sharepoint online the cmdlet are not recognized, how do i go about it any help really appreciable