Remove all items from a SharePoint Online list using PnP PowerShell

In this PnP PowerShell tutorial, we will discuss, how to remove all items from a SharePoint Online list using PnP PowerShell.

If you are new to PnP PowerShell, you can read an article on How to connect SharePoint Online using PnP PowerShell.

Remove all items from a SharePoint Online list using PnP PowerShell

Here, I got a requirement to delete more than 10000 items from a SharePoint Online list.

I used PnP PowerShell to delete items from the SharePoint Online list.

Below is the PowerShell command to remove all items from a SharePoint Online list using PnP PowerShell, you can write, debug and test the script using PowerShell ISE.

Connect-PnPOnline –Url https://tsinfo.sharepoint.com/sites/sharepointsky/

$items =Get-PnPListItem -List "Budgets" -PageSize 500

foreach ($item in $items)
{
try
{
Remove-PnPListItem -List "Budgets" -Identity $item.Id -Force
}
catch
{
Write-Host "Error Occurred While Deleting the Item from the SharePoint Online List"
}
}

This will delete all the items from the list.

It will take really good amount of time if it is a large list, or is having more items in the SharePoint list.

Here I have added the -PageSize 500 attribute.

Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

If you will not use the -PageSize attribute, then you will get the error like below:

Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

The above error comes because of the SharePoint list view threshold issue. To fix the error you can add the -PageSize like below:

$items =Get-PnPListItem -List "Budgets" -PageSize 10000

You may like following PnP PowerShell tutorials:

In this tutorial we learned how to Remove all items from a SharePoint Online list using PnP PowerShell and how to fix error Get-PnPListItem : The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

>