SharePoint Online: Bulk Delete All List Items using PowerShell

Requirement: Bulk Delete SharePoint Online List Items using PowerShell.

Bulk Delete All Items in SharePoint Online List using PowerShell

Delete All List Items in SharePoint Online using PowerShell:

We have a list in SharePoint Online – Office 365 site with a bunch of items. Need them to be deleted in bulk rather than deleting them one by one from SharePoint web UI. Here is the script to delete list items in bulk in SharePoint Online Office 365 using Client Side Object Model (CSOM) with PowerShell.

#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"
  
#Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName="LargeList"
$BatchSize = 100

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the List
    $List=$Ctx.web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total Number of Items Found in the List:"$List.ItemCount

    Write-Host -f Yellow -NoNewline "Deleting List Items from List '$ListName'"
  
    #Set a Flag
    $Continue = $True
    While($Continue)
    {
        Write-Host -NoNewline -f Yellow "."

        #Get List Items in Batches
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery($BatchSize, "ID")
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
                
        If($ListItems.Count -gt 0)
        {
            #Delete List Items
            for($i = $ListItems.Count-1; $i -ge 0; $i--)
            {
                $ListItems[$i].DeleteObject()
            }
            $Ctx.ExecuteQuery()
        }
        Else
        {
            $Continue = $False
        }
    }
    Write-Host -f Green "`nAll List Items Deleted Successfully!"
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Please note, these list items are not moved to the Recycle Bin but deleted permanently! This PowerShell script could be quite helpful when dealing with deleting bulk items from a huge list. Here is another PowerShell to delete all list items in SharePoint Online: Delete All Items from SharePoint Online List 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: Bulk Delete All List Items using PowerShell

Leave a Reply

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