SharePoint Online: Delete All List Items using PowerShell

Requirement: Delete All Items from SharePoint Online List using PowerShell.

We have a list in SharePoint Online – Office 365 site with a bunch of items and need them to be deleted in bulk rather than deleting one by one from SharePoint web UI. So, let us use SharePoint Online PowerShell to delete all list items. This blog post will show you how to use PowerShell to delete all list items in a SharePoint Online site.

delete all list items in sharepoint online using powershell

SharePoint Online: How to Delete All Items in a List?

Lists are a core component of SharePoint Online, providing users with a flexible and customizable way to store and manage data. However, there may be times when you need to delete items from a list, either to clean up old data, or for other reasons.

How to delete all list items in SharePoint Online? To delete all items from a SharePoint Online list, do the following:

  1. Browse to your list, and select all items in the list by ticking the checkbox beside the first column on the list. This should highlight all items in the list view. 
  2. Click the “Delete” button in the list’s menu bar.
    sharepoint online delete all list items
  3. Confirm the prompt. The selected item will be sent to the recycle bin.

SharePoint allows you to select 100 items at once! However, in modern lists, you can select all items by scrolling to the bottom of the page – so that SharePoint loads and selects the remaining items. What if you have a significantly larger list? Well, PowerShell is the time-saver here!

If you don’t get the check boxes, Edit the list view >> Under the Tabular View section make sure “Allow individual item checkboxes” is selected!

Delete All List Items in Bulk in SharePoint Online using PowerShell:

If you’re managing SharePoint Online, there’s a good chance you’ll need to perform bulk deletes at some point. This blog post will show you how to use PowerShell to perform bulk delete items in a SharePoint Online list. Here is the script to delete all list items in SharePoint Online Office 365 using the 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"
  
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Get Credentials to connect 
$Cred = Get-Credential

#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 items to delete
$List = $Ctx.web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()       
 
write-host "Total Number of List Items found:"$ListItems.Count
 
#SharePoint Online PowerShell to delete all list items
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 "All List Items deleted Successfully!"
} 

This PowerShell script deletes all items from the SharePoint Online list. Please note, these items are not moved to the Recycle Bin but deleted permanently! This PowerShell script could be quite helpful when deleting bulk items from a huge list.

PowerShell to Delete All Items from Large Lists in SharePoint Online

When you have a SharePoint list or library with a larger number of items (>5000), You may face a “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.” error. So, to mitigate this issue, let us delete the list of items in the batch. Here is my PowerShell script to delete list items in the batch.

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"
    
#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$BatchSize = 500
  
#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 web and List
    $Web = $Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total Number of Items Found in the List:"$List.ItemCount
 
    #Define CAML Query to get list items in batches
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
 
    Do {  
        #Get items from the list in batches
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
         
        #Exit from Loop if No items found
        If($ListItems.count -eq 0) { Break; }
 
        Write-host Deleting $($ListItems.count) Items from the List...
 
        #Loop through each item and delete
        ForEach($Item in $ListItems)
        {
            #Delete SharePoint list items
            $List.GetItemById($Item.Id).DeleteObject()
        } 
        $Ctx.ExecuteQuery()
 
    } While ($True)
 
    Write-host -f Green "All Items Deleted!"
}
Catch {
    write-host -f Red "Error Deleting List Items!" $_.Exception.Message
}

This script deletes all list items in SharePoint Online using PowerShell.

The “DeleteObject()” method permanently deletes the item. Use “Recycle()” to send the item to recycle bin.

SharePoint Online PnP PowerShell to Delete All List Items

Here is the PnP PowerShell to empty the list in SharePoint Online.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName ="Records"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Get All List Items in Batch
    $ListItems = Get-PnPListItem -List $ListName -PageSize 1000 | Sort-Object ID -Descending

    #sharepoint online powershell delete all items in a list
    ForEach ($Item in $ListItems)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.Id -Force
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Use -Recycle parameter to send the items to the recycle bin instead of permanently deleting them!

A Faster Way to Delete List Items in Bulk

The above methods are slow and delete one item in 1 second approx. However, the script block method below deletes the items faster.

Get-PnPList -Identity $ListName | Get-PnPListItem -PageSize 100 -ScriptBlock { 
    Param($items) Invoke-PnPQuery } | ForEach-Object {$_.Recycle()
 }

Clear List Items in SharePoint Online Faster with PnP Batch Method

The New-PnPBatch method relatively performs bulk operations faster. Here is an example of removing all items with the PnP Batch method.

$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$ListName = "Inventory"

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

#Get List Items to Delete
$ListItems =  Get-PnPListItem -List $ListName -PageSize 500

#Create a New Batch
$Batch = New-PnPBatch

#Clear All Items in the List
ForEach($Item in $ListItems)
{    
     Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
}

#Send Batch to the server
Invoke-PnPBatch -Batch $Batch

Wrapping up

In summary, following the steps outlined in this article, you can easily delete all list items from a SharePoint Online list using PowerShell. This approach can help you save time and effort, and ensure that your data is managed effectively, especially when you have a large list with many items. To delete all files from a SharePoint Online document library, use: SharePoint Online: Delete All Files in a Document Library using PowerShell

How do I delete a file from SharePoint Online in PowerShell?

To delete a file from SharePoint Online in PowerShell, you can use the Remove-PnPFile cmdlet from the SharePoint Patterns and Practices (PnP) PowerShell module. Here’s an example PowerShell script that deletes a file from a document library in SharePoint Online: Remove-PnPFile -ServerRelativeUrl “/sites/yoursite/Shared Documents/yourfile.docx”

How do I delete a folder in OneDrive using PowerShell?

Use the Remove-PnPFolder cmdlet in PnP Powershell to delete a Folder in OneDrive for Business. E.g.,
Connect-PnPOnline -Url “https://Crescent-my.sharepoint.com/personal/salaudeen_Crescent_com” -Interactive
Remove-PnPFolder -Name “Backup” -Folder “Documents”

How do I delete a document library in SharePoint Online?

To delete a document library in SharePoint Online, you can use either the SharePoint Online web interface or PowerShell.
To delete a document library in SharePoint Online using the web interface, follow these steps: Navigate to the SharePoint site and the document library to delete. Click on the settings gear icon in the top-right corner and select “Library Settings” from the dropdown menu. Click on “More Library Settings” and then “Delete this Document Library” link under document library settings. You can also delete a document library in SharePoint Online using the PowerShell cmdlet using the Remove-PnPList.
More info: How to delete a document library in SharePoint Online?

How do I delete a SharePoint list in PowerShell?

To delete a list using PowerShell, use: Remove-PnPList -Identity “List Name” -Force
More info: How to delete a list in SharePoint Online 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!

15 thoughts on “SharePoint Online: Delete All List Items using PowerShell

  • Thanks for this Salaudeen, Much appreciated.

    Reply
  • How do i use this script if i have a 2FA enabled? Thank you

    Reply
  • Hi,

    Thank you very much for this script.

    Unfortunately, I am not able to use this for a Folder under a Documents Library.

    It works for Lists though, like a charm.

    Did I miss something here?

    Reply
    • Are you getting “Remove-PnPListItem : Item does not exist. It may have been deleted by another user.”? Try to sort the list items by ID field in descending order.
      $ListItems = Get-PnPListItem -List $ListName -PageSize 1000 | Sort-Object ID -Descending

      Reply
  • I’m trying to use the batch mode script but it’s not limiting the view, I’m still getting the

    Error Deleting List Items! Exception calling “ExecuteQuery” with “0” argument(s): “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.”

    There are 174492 items in my sharepoint Document’s folder. Please advise.

    I tried adjusting from 500 to 100 but no change.

    Reply
    • Found HTML entities were removed in the rendered script in the site and fixed the same. Try now!

      Reply
  • Great article, helped me significantly

    Reply
  • Thank you for advice! I suppose error appears only at first several attempts while list had large amount of entries. After deleting several thousands, scripts started working more stably.

    Reply
  • Second script for large lists works fine except this error: “Error Deleting List Items! Exception calling “ExecuteQuery” with “0” argument(s): “The operation has timed out.” It makes me execute script constantly

    Reply
  • How do I delete all Files (Not Folders)?

    Reply
    • Add an If condition:
      If($ListItem.FileSystemObjectType -eq “File”)
      {
      #Delete the Item
      }

      Reply
  • Hi
    Great article really helped. When running the script it gave an error when the list items are greater than 5000. Any idea how we can overcome this?

    Reply

Leave a Reply

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