How to Delete All Items from a SharePoint List using PnP PowerShell?

Recently, I got an urgent requirement to delete all the items from a SharePoint Online list. PowerShell is the best option for bulk deleting list items. In this tutorial, I will show you how to delete all items from a SharePoint list using PnP PowerShell and PowerShell.

I recommend using PnP PowerShell to delete items from the SharePoint list.

From all the examples, I will use the SharePoint Online list below. The list has a few items, as shown in the screenshot below.

delete all items from a SharePoint list using powershell

Delete All Items from a SharePoint Online List using PnP PowerShell

PnP PowerShell provides efficient ways to work with SharePoint lists.

Here is the complete script to delete all items from a SharePoint list using PnP PowerShell.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"

# Get all items in the list
$items = Get-PnPListItem -List $listName

# Loop through and delete each item
foreach ($item in $items) {
    Remove-PnPListItem -List $listName -Identity $item.Id -Force
}

I executed the above script using VS code, and you can see in the screenshot below that all the SharePoint list items have been deleted.

delete sharepoint list items using pnp powershell

While deleting SharePoint list items, you can also define the page size. Here is a complete script.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"

# Get all items in the list
$items = Get-PnPListItem -List $listName -PageSize 2000

# Loop through and delete each item
foreach ($item in $items) {
    Remove-PnPListItem -List $listName -Identity $item.Id -Force
}

# Disconnect the session
Disconnect-PnPOnline

Read Import Data from Excel to SharePoint List using PnP PowerShell

Batch Delete SharePoint Large List Items using PnP PowerShell

If you are deleting all items from a large SharePoint list, the process can be a little slow. Batch deletion can significantly speed up the process. Using PnP PowerShell, it is easy to delete items in batch.

Here is the complete code.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"
# Get all items in the list
$items = Get-PnPListItem -List $listName

# Create a batch
$batch = New-PnPBatch

# Queue deletion of each item
foreach ($item in $items) {
    Remove-PnPListItem -List $listName -Identity $item.Id -Batch $batch -Force
}

# Execute the batch
Invoke-PnPBatch -Batch $batch

Here:

  • The Connect-PnPOnline cmdlet with the -Interactive parameter will prompt you to log in interactively.
  • Get-PnPListItem retrieves the list items, and -PageSize 2000 helps handle larger lists.
  • Remove-PnPListItem is used to delete each item.

This is how to delete items from a large SharePoint Online list using PnP PowerShell with a batch.

Read Add More Than 5000 Items to a SharePoint Online List using PnP PowerShell

Delete All Items from a SharePoint List using PowerShell

In the first method, I will show you how to delete all items from a SharePoint Online list using PowerShell (SharePoint Online Management Shell). If you are new to this, you can download and install SharePoint Online Management Shell first.

Below is the complete PowerShell script to delete all items from the SharePoint list.

$siteUrl = "https://szg52.sharepoint.com/sites/MarketingTeam"
$listName = "ProjectTasks"

# Connect to the site
Connect-SPOService -Url $siteUrl

# Get the list
$list = Get-SPOList -Identity $listName

# Get all items in the list
$items = Get-SPOListItem -List $list

# Loop through and delete each item
foreach ($item in $items) {
    Remove-SPOListItem -List $list -Identity $item.Id -Confirm:$false
}

Once you execute the above script, all the items from the SharePoint list will be deleted.

In this tutorial, I have explained how to delete all items from a SharePoint Online list using PnP PowerShell and how to delete all items in batches from a large SharePoint list using PnP PowerShell.

If you still have questions, feel free to leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.