SharePoint Online: Delete a List Item using PowerShell

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

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 "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 
If you want to delete all items from SharePoint Online list using PowerShell, use: SharePoint Online: Delete All List Items using PowerShell

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 lit 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 recycle bin!
SharePoint Online: Delete a List Item using PowerShell SharePoint Online: Delete a List Item using PowerShell Reviewed by Salaudeen Rajack on January 03, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.