
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() } }
No Comments