Deleting List Items using PnP PowerShell

Loading

Deleting list items in SharePoint Online can be done efficiently using PnP PowerShell. Whether you need to remove a single item, multiple items, or bulk delete items based on conditions, this guide covers everything you need to know.

What you will learn:

  • How to connect to SharePoint Online
  • How to delete a single list item
  • How to delete multiple items
  • How to delete list items based on conditions
  • How to bulk delete items from a CSV file

Prerequisites

Before running PowerShell scripts, ensure that:
PnP PowerShell is installed
You have Delete permissions on the SharePoint list
You know the SharePoint site URL and list name


Step 1: Install and Import PnP PowerShell

If you haven’t installed PnP PowerShell, install it using:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint Online

Before deleting list items, connect to your SharePoint site:

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your tenant name
🔹 Replace "yoursite" with the site name

Connection successful!


Step 3: Delete a Single List Item

To delete a specific list item by ID, use:

# Delete a single item from the SharePoint list
Remove-PnPListItem -List "Employee Directory" -Identity 5 -Force

🔹 The Identity parameter refers to the item ID
🔹 The -Force parameter deletes the item without confirmation

Item deleted successfully!


Step 4: Delete Multiple List Items

If you need to delete multiple items, use a loop:

# Get all items where Department is "HR" and delete them
$items = Get-PnPListItem -List "Employee Directory" | Where-Object { $_["Department"] -eq "HR" }

foreach ($item in $items) {
Remove-PnPListItem -List "Employee Directory" -Identity $item.Id -Force
}

All HR department items deleted!


Step 5: Delete Items Based on Conditions

To delete list items that meet a specific condition, use filtering:

# Delete all employees who joined before 2023
$oldEmployees = Get-PnPListItem -List "Employee Directory" | Where-Object { $_["JoiningDate"] -lt "2023-01-01" }

foreach ($emp in $oldEmployees) {
Remove-PnPListItem -List "Employee Directory" -Identity $emp.Id -Force
}

Filtered items deleted!


Step 6: Bulk Delete Items from a CSV File

If you have a list of items to delete stored in a CSV file, follow this approach.

CSV File (delete-employees.csv)

ID
3
7
12

PowerShell Script to Delete from CSV

# Import CSV data
$employees = Import-Csv "C:\delete-employees.csv"

# Delete each employee record
foreach ($emp in $employees) {
Remove-PnPListItem -List "Employee Directory" -Identity $emp.ID -Force
}

Bulk delete completed from CSV!


Step 7: Verify Deleted Items

To check if the items were deleted, list the remaining items:

powershellCopyEdit# Get the last 5 items in the list
Get-PnPListItem -List "Employee Directory" -PageSize 5 | Select Title, Department, JoiningDate

Deleted items verified!


Common Errors & Troubleshooting

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you have Delete rights on the list
Cannot find list 'Employee Directory'Incorrect list nameUse Get-PnPList to verify the list name
Item not foundIncorrect item IDEnsure you are using the correct ID

Leave a Reply

Your email address will not be published. Required fields are marked *