Did you ever wanted to quickly delete all items from a SharePoint list without having to run into complex scenarios?
This can be simply achieved using PnP Powershell (obviously!). And it fits in a single line!
If you are looking for options to delete documents from SharePoint document libraries, maybe this post can help.
Delete SharePoint list items
Connect to the site using Connect-PnPOnline and then run:
Get-PnPList -Identity Lists/MyList | Get-PnPListItem -PageSize 100 -ScriptBlock { Param($items)
$items.Context.ExecuteQuery() } | % {$_.DeleteObject()}

Hello, Joel
Hope you are doing well.
And how about doing a Filter on $Items before a deleting action ??
Warm regards.
Hi Mario, I think a filter would work just fine. You should be able to do it straight from the Get-PnPListItem command: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnplistitem?view=sharepoint-ps
Hello, Joel
Hope you are doing well.
And how about doing a Filter on $Items before a deleting action ??
Warm regards.
Hi Mario, I think a filter would work just fine. You should be able to do it straight from the Get-PnPListItem command: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnplistitem?view=sharepoint-ps
Hi Joel,
I have the same problem and at first came to the same solution. But then I thought:
“$_.DeleteObject()” runs for each item… Is there a way to run “delete” on the server side in a batches?
So i tried:
Get-PnPListItem -List $list -Fields “ID” -PageSize 100 -ScriptBlock { Param($items) $items | Sort-Object -Descending | ForEach-Object{ $_.DeleteObject() } }
and it worked. Thougts?
Hi Joel,
I have the same problem and at first came to the same solution. But then I thought:
“$_.DeleteObject()” runs for each item… Is there a way to run “delete” on the server side in a batches?
So i tried:
Get-PnPListItem -List $list -Fields “ID” -PageSize 100 -ScriptBlock { Param($items) $items | Sort-Object -Descending | ForEach-Object{ $_.DeleteObject() } }
and it worked. Thougts?
Hi Vladilen,
That looks fine to me and may be much better in terms of performance. Have you tried to delete 1000+ items to ensure that it does not time out? If not, that’s great and thanks for sharing here
Hi Vladilen,
That looks fine to me and may be much better in terms of performance. Have you tried to delete 1000+ items to ensure that it does not time out? If not, that’s great and thanks for sharing here
I have tried the script to delete more 300k items in a list and I had issues with time out with errors like
Get-PnPListItem : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
At C:\Users\rauckloo\Scripts\deleteAllItemsFromLists.ps1:24 char:1
+ Get-PnPListItem -List $listName -Fields “ID” -PageSize 100 -ScriptBlo …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Get-PnPListItem], CollectionNotInitializedException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem
Each time, I encountered the error at varying amount of times, I just re run the script again. It took +7 hours to delete 330k items from a list. Also if the list has less items the page size specified, e.g. Page size is 10 and number of items remaining in list is 5, the 5 items don’t get deleted.
Hi Rashmee
Thanks for the feedback, the script was never really tested with such large numbers.
I believe something like the following would have a better performance
Get-PnPList -Identity $ListName | Get-PnPListItem -PageSize 100 -ScriptBlock {
Param($items) Invoke-PnPQuery } | ForEach-Object {$_.Recycle()
}
#Read more: https://www.sharepointdiary.com/2015/10/delete-all-list-items-in-sharepoint-online-using-powershell.html#ixzz77tBGiXom
I have tried the script to delete more 300k items in a list and I had issues with time out with errors like
Get-PnPListItem : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
At C:\Users\rauckloo\Scripts\deleteAllItemsFromLists.ps1:24 char:1
+ Get-PnPListItem -List $listName -Fields “ID” -PageSize 100 -ScriptBlo …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Get-PnPListItem], CollectionNotInitializedException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem
Each time, I encountered the error at varying amount of times, I just re run the script again. It took +7 hours to delete 330k items from a list. Also if the list has less items the page size specified, e.g. Page size is 10 and number of items remaining in list is 5, the 5 items don’t get deleted.
Hi Rashmee
Thanks for the feedback, the script was never really tested with such large numbers.
I believe something like the following would have a better performance
Get-PnPList -Identity $ListName | Get-PnPListItem -PageSize 100 -ScriptBlock {
Param($items) Invoke-PnPQuery } | ForEach-Object {$_.Recycle()
}
#Read more: https://www.sharepointdiary.com/2015/10/delete-all-list-items-in-sharepoint-online-using-powershell.html#ixzz77tBGiXom