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.  Smile

# 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

10 Comments

    1. 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.

  1. 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.

  2. This is not working for sharepoint online the cmdlet are not recognized, how do i go about it any help really appreciable

Leave a Comment

Return to Top ▲Return to Top ▲