Delete All SharePoint List Items using PowerShell

When working with the SharePoint Online list, there are many ways to delete a single item or all list items, like manually or programmatically.

In the SharePoint tutorial, I will show you how to delete a single list item in SharePoint using PnP PowerShell and how to delete all list items in SharePoint list using PnP PowerShell.

Also, I will discuss how to get all list items in SharePoint using PnP PowerShell and how to get a single list item in SharePoint using PnP PowerShell with real-time examples.

SharePoint Online PnP Delete List Item

In PowerShell, the commands Remove-PnPListItem or Move-PnPListItem ToRecycleBin are used for deleting list items within the SharePoint Online list. To execute the PowerShell script, you can use an editor such as “Visual Studio Code.”

Syntax of the Remove-PnPListItem command:

Remove-PnPListItem [-List] <ListPipeBind> -Identity <ListItemPipeBind> [-Recycle] [-Force] 

Where,

  • List = Provide the list name where you want to delete the item
  • Identity = It is the ID of the list item or actual ListItem object
  • Recycle = SharePoint list, deleted items stored in the recycle bin

Example:

I have a SharePoint Online list named “Expense Tracker” that has some records or items. Now, I would like to delete the third item [ID = 3] using PnP PowerShell.

pnp delete list item

For that, use the PowerShell command below to delete a single list item in SharePoint with the parameters List, Identity, and Force.

Connect-PnPOnline -Url https://szg52.sharepoint.com/sites/Marketing

Remove-PnPListItem -List "Expense Tracker" -Identity "3" -Force
remove pnplistitem

Now, execute the above command by clicking the RUN button. Then, you will see the details of deleting a single Sharepoint list item in the TERMINAL, as shown below.

delete sharepoint site powershell

Finally, The specific SharePoint list item [Item with ID number 3] was deleted, as shown below.

sharepoint powershell delete list

Syntax of the Move-PnPListItem ToRecycleBin command:

Move-PnPListItemToRecycleBin [-List] <ListPipeBind> -Identity <ListItemPipeBind> [-Force] 
 [-Connection <PnPConnection>] 

Example:

See also  How to Convert Multiline String to Array in PowerShell?

Similarly, in this example, I will show you how to delete the third item [ID = 3] using the PnPListItemToRecycleBin command. Follow the code below.

Connect-PnPOnline -Url https://szg52.sharepoint.com/sites/Marketing

Move-PnPListItemToRecycleBin -List "Expense Tracker" -Identity "8" -Force

Then, execute the above command by clicking the RUN button. Then, you will see the details of deleting a single Sharepoint list item in the TERMINAL, as shown below.

delete item powershell

Finally, have a look at the below screenshot for the output.

delete-item powershell

This is how we can delete a single list item in SharePoint using PowerShell.

Delete All SharePoint List Items using PnP PowerShell

In this example, I have taken the Remove-PnPListItem command with the Batch parameter in the ForEach loop to delete all list items in SharePoint.

Also, I have used the Invoke-PnPBatch command to send the batch request to the SharePoint server. Follow the code below.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Marketing"
$ListName = "Expense Tracker"
$ListItems =  Get-PnPListItem -List $ListName -PageSize 500

#Clear All Items in the List
ForEach($Item in $ListItems)
{   
     Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
}
 
#Send Batch request to the server
Invoke-PnPBatch -Batch $Batch

Then, execute the above command by clicking the RUN button. Then, you will see the details of deleting all Sharepoint list items in the TERMINAL, as shown below.

delete all items from sharepoint list

Now, have a look at the below screenshot for the output.

Output:

delete all items in sharepoint list

This is how we can delete or remove all list items in SharePoint using PnP PowerShell

SharePoint Online Get-PnP List Items

In this section, I will explain how to get all list items in SharePoint using PnP PowerShell.

In PowerShell, the Get-PnPListItem command is used to get all items from the SharePoint list. Also, we can use parameters to specify the list, fields, page size, script block, content type, include, and connection while getting items from the SharePoint list.

See also  How to Pass Objects to Functions in PowerShell? [Multiple Examples]

Syntax of the Get-PnP List Items:

Get-PnPListItem [-List] <ListPipeBind> 
  [-Fields <String[]>]
  [-PageSize <Int32>]
  [-ScriptBlock <ScriptBlock>]
  [-IncludeContentType <SwitchParameter>]
  [-Connection <PnPConnection>]

Where,

  • Get-PnPListItem = This command is used to retrieve items from the SharePoint list
  • List = This parameter specifies the SharePoint list from which you want to get items. You can provide a list name or GUID here
  • Fields = This is an optional parameter. You can specify the column names to retrieve items or provide an array of column names
  • PageSize = It is an optional parameter. You can specify the number of items to get per page. Use this parameter when you have more items in the SharePoint list
  • ScriptBlock = This parameter is optional. It can be used to filter the items and perform custom processing, such as formatting
  • IncludeContentType = We can use an optional parameter to get the content type of each item
  • Connection = This optional parameter allows you to specify the PnP connection. It is useful when working with multiple connections

Example:

I have a SharePoint Online list named “Vacation Budget” that has some records or items.

Input:

get-pnplistitem pagesize

Now, I would like to get all these SharePoint list items using PnP PowerShell. For that, you can use the Get-PnP List Items command and provide the SharePoint Site URL and List name, as shown below.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Marketing"
Get-PnPListItem -List "Vacation Budget"
get pnplistitem

Then, execute the above command by clicking the RUN button. Then, you will get all Sharepoint list items in the TERMINAL, as shown below.

get-pnplistitem where-object

This is how we can get all Sharepoint list items using PnP PowerShell.

See also  How to Add Comments in PowerShell: Complete Guide

How to Get-PnP SharePoint List Item

In the last, I will show you how to get the specific SharePoint list item using PnP PowerShell.

Example:

Suppose you want to get the first [Item ID is = 1] item from the SharePoint Online list [Vacation Budget] using PnP PowerShell; you can use the code below.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Marketing"
Get-PnPListItem -List "Vacation Budget" -Id 1

Once, execute the above command by clicking the RUN button. Then, you will get the specific Sharepoint list item in the TERMINAL, as shown below.

get-pnplistitem the attempted operation is prohibited because it exceeds the list view threshold

This way, we can work with how to get the specific SharePoint list item using PnP PowerShell.

I hope this SharePoint tutorial is useful. If you have any requirements related to how to delete all list items in SharePoint using PnP PowerShell or how to get all list items in SharePoint using PnP PowerShell, you can follow the above examples to achieve them.

You may also like:

>