Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
No terms - use on your own risk!
This script efficiently emtpties are large list and is approximately 30 times faster than deleting item by item.
You need to have the SharePoint Online SDK ( installed to run this script.
SharePoint Online web URL, for example ''.
.PARAMETER listname
Name of the list that should be emptied
Max Melcher (@maxmelcher) - more on my blog
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$weburl,[Parameter(Mandatory=$true,ValueFromPipeline=$true)]$listname, $username, $password)
function empty-list($weburl, $listname)
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if (!$clientContext.ServerObjectIsNull.Value)
Write-Host "Connected to SharePoint Online web: '$webUrl'" -ForegroundColor Green
#global counter
$spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
#create a simple batch. I received the best results with 500 items per batch.
#5000 is the maximum, but i received so many timeouts.
$spQuery.ViewXml = "<View><Query></Query><ViewFields><FieldRef Name='Id' /></ViewFields><RowLimit>500</RowLimit></View>"
#track the performance
$watch = [System.Diagnostics.Stopwatch]::StartNew()
#get the list
$list = $clientContext.Web.Lists.GetByTitle($listname);
#get the current batch of items
$items = $list.GetItems($spQuery);
#get a batch and delete the previous items in one go
$count = $items.Count
Write-Host "...deleting $count items" -ForegroundColor Yellow
while ($items.Count -gt 0)
if ($counter%500 -eq 0)
Write-Progress -Activity "Empty List '$listname'" -status "Item $counter of $total marked for deletion"
#iterate the collection
$spQuery.ListItemCollectionPosition = $items.ListItemCollectionPosition
while ($spQuery.ListItemCollectionPosition -ne $null)
#delete the last batch
$seconds = ($watch.ElapsedMilliseconds/1000)
$secondsf = "{0:N0}" -f $seconds
$ips = $counter / $seconds
write-host -ForegroundColor Green "Done! deleted $counter items in $secondsf seconds. $ips items per second"
Write-Host -ForegroundColor Red $_.Exception.ToString()
# change to the path of your CSOM dlls and add their types
$csomPath = "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI"
Add-Type -Path "$csomPath\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$csomPath\Microsoft.SharePoint.Client.Runtime.dll"
if([String]::IsNullOrWhiteSpace($username)) {
$username = Read-host "What's your username?"
if([String]::IsNullOrWhiteSpace($password)) {
$securePassword = Read-host "What's your password?" -AsSecureString
} else {
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
Empty-List -weburl $weburl -listname $listname -user $username -password -securePassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment