Freigeben über


Bulk delete SharePoint Online List Items

By: Fabiano Mira
We know that using large lists with SharePoint Online can cause some problems due the 5000 item List View Threshold, and it is completely understandable why this limit exists, it´s to minimize database contention SQL Server, the back-end database for SharePoint, often uses row-level locking as a strategy to ensure accurate updates without adversely impacting other users who are accessing other rows.
There are several ways to manage large lists that can be find in this article:

Manage large lists and libraries in SharePoint
https://support.office.com/en-us/article/Manage-large-lists-and-libraries-in-SharePoint-B8588DAE-9387-48C2-9248-C24122F07C59

Just in case you get throttled and the list become instable or you can´t access it anymore to create your views you can clean the items to open the list again.
Usually this applies to historical lists where the data can be erased.
Here it is an CSOM PowerShell Script that you can easily use to delete all list items on a bulk operation.

  
#Change here with your local path with SP DLLs downloaded from SP Framerwork
Import-Module 'C:\temp\SharePoint Online Client Browser v2.2\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\temp\SharePoint Online Client Browser v2.2\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\temp\SharePoint Online Client Browser v2.2\Microsoft.SharePoint.Client.dll'


$site = ''
$admin = ''

$password = Read-Host 'Enter Password' -AsSecureString

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials


$list = $context.Web.Lists.GetByTitle('')
$context.Load($list)
$context.ExecuteQuery()
$count = $list.ItemCount

$newline = [environment]::newline
Write-Host -NoNewline "Deleting listitems" -foregroundcolor black -backgroundcolor yellow
$continue = $true
while($continue)
{
    Write-Host -NoNewline "." -foregroundcolor black -backgroundcolor yellow
    $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(100, "ID")
    $listItems = $list.GetItems( $query )
    $context.Load($listItems)
    $context.ExecuteQuery()       
    if ($listItems.Count -gt 0)
    {
        for ($i = $listItems.Count-1; $i -ge 0; $i--)
        {
            $listItems[$i].DeleteObject()
        } 
        $clientContext.ExecuteQuery()
    }
    else
    {
        Write-Host "." -foregroundcolor black -backgroundcolor yellow
        $continue = $false;
    }
}
Write-Host "All listitems deleted from Pages." -foregroundcolor black -backgroundcolor green