SharePoint Online: Delete a List Item using PowerShell

Requirement: Delete a List Item in SharePoint Online.
delete list item in sharepoint online using powershell

How to Delete a List Item in SharePoint Online?
To delete a list item from SharePoint Online list, do the following:
  • Browse to your list, select the item you would like to delete. 
  • Click the "Delete" button in the list's menu bar.
    sharepoint online delete list item
  • 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 choose "Delete" from the context menu.

SharePoint Online PowerShell to Delete List Item
Here is my PowerShell script to delete an item from 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 List Item in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName ="Projects"
$ListItemID=7

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

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

Delete List Items in SharePoint Online using PnP PowerShell
Similarly, You can delete list items based on filtering field values.
#Config Variables
$SiteURL = "https://crescenttech.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
}
You can also use Move-PnPListItemToRecycleBin cmdlet to move a list item to the recycle bin! If you want to delete all items from SharePoint Online list using PowerShell, use: SharePoint Online: Delete All List Items using PowerShell

2 comments:

  1. how to delete list items created by a specific user?

    ReplyDelete
    Replies
    1. Get list items created by a specific user with CAML query or filtering list items based on given user as in PowerShell to Get All List Items Created or Modified by a Specific User in SharePoint Online

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.