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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply