Deleting items from large lists sometime get really challenging specially from those which crossed the threshold limit of 5000 items. It easier to delete items from listview in modern interface but you cannot delete a large no. of items. There could be different scenarios for deleting large number of items based metadata settings or deleting test entries.

We got a scenario where workflows got too slow due large size of workflow history list. And deletion was not working for us.

Solution

The easiest way to delete item is to select the item(s) from list view and hit delete button. But for large batch, I would recommend PnP PowerShell cmdlet. Below example deletes last 10 items from a SharePoint list.

$siteURL = "https://mstalk.sharepoint.com/sites/sitename"
$listName = "Issue Tracking"
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
#Get the last 20 row items
$items = Get-PnPListItem -List $listName | select -Last 10
forEach ($item in $items)
{
   #removes the item and saves it in the Recycle Bin
   remove-PnPListItem -List $listName -Identity $item.ID -Force -Recycle
   write-Host "Deleted the Item Title : " $Item["Title"] 
}

Deleting List Items in batches

Use below cmdlet to delete list items in batches, here list items will be deleted in batch of 1000 items.

Get-PnPListItem -List $listName -Fields "ID" -PageSize 1000 -ScriptBlock 
{ Param($items) $items | Sort-Object -Property Id -Descending | ForEach-Object{ $_.DeleteObject() } }

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply