SharePoint Online: Delete a List Item using PowerShell

Requirement: Delete a List Item in SharePoint Online using PowerShell.

delete list item in sharepoint online using powershell

In this blog post, we’ll show you how to use PowerShell to delete a list item from your SharePoint Online list. We’ll share some PowerShell examples to delete a list item by ID, delete a list item by Title, and delete list items matching given conditions. We will walk through the steps needed to connect to PowerShell and then delete an item from a list. Let’s get started!

How to Delete a List Item in SharePoint Online?

To delete a list item from the SharePoint Online list, do the following:

  1. Browse to your list, and select the item you would like to delete. 
  2. Click the “Delete” button in the list’s menu bar.
    sharepoint online delete list item
  3. Confirm the prompt. The selected item will be sent to the recycle bin.

You can also delete the list item by right-clicking on it and choosing “Delete” from the context menu.

SharePoint Online PowerShell to Delete List Item

PowerShell is a versatile tool that can be used to manage and automate tasks in SharePoint Online. Let’s see how to use PowerShell to delete a list item. This can be useful if you need to quickly remove some data from a list or something in the list that you no longer need. Here is my PowerShell script to delete an item from the SharePoint Online list:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Delete-ListItem()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List and item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem = $List.GetItemById($ItemID)
        $ListItem.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host "List Item Deleted successfully!" -ForegroundColor Green

    }
    Catch {
        write-host -f Red "Error Deleting List Item!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="1"

#Call the function to rename column
Delete-ListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID

SharePoint Online: Bulk Delete List Items Matching Given Condition

How to bulk delete SharePoint Online list items using PowerShell? Say, we want to delete all inactive projects from the “Projects” list.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Delete-ListItems()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $CAMLQuery
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Define Query to get List Items
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml =$CAMLQuery
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get All List Items matching the query
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        Write-host "Total Number of Items Found to delete: $($ListItems.count)"
        if($ListItems.count -gt 0)
        {
            #Loop through each item and delete
            For ($i = $ListItems.Count-1; $i -ge 0; $i--)
            {
                $ListItems[$i].DeleteObject()
            } 
            $Ctx.ExecuteQuery()

            Write-Host "List Items Deleted successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Deleting List Items!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CAMLQuery="<View><Query><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>0</Value></Eq></Where></Query></View>"

#Call the function to rename column
Delete-ListItems -SiteURL $SiteURL -ListName $ListName -CAMLQuery $CAMLQuery 

PnP PowerShell to Delete a List Item in SharePoint Online

To delete a particular list item by its ID, use this PnP PowerShell cmdlet Remove-PnPListItem:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemID=7

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Remove List Item by ID
Remove-PnPListItem -List $ListName -Identity $ListItemID -Recycle -Force

Delete List Item By Title in SharePoint Online using PnP PowerShell

Similarly, You can delete list items based on filtering field values. You can also use the Move-PnPListItemToRecycleBin cmdlet to move a list item to the recycle bin!

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemTitle="SharePoint Online Migration Project"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the List Item by "Title" field value
$Query = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$ListItemTitle</Value></Eq></Where></Query></View>"
$ListItem = Get-PnPListItem -List $ListName -Query $Query -PageSize 1

#Removes the List item
If($ListItem -ne $null)
{
    Remove-PnPListItem -List $ListName -Identity $ListItem -Force
}

How do I delete all items from a SharePoint list in PowerShell? If you want to delete all items from the SharePoint Online list using PowerShell, use: SharePoint Online: Delete All List Items using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Delete a List Item using PowerShell

Leave a Reply

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