How to delete large lists in Sharepoint online using Powershell

Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
$web = get-spweb "https://mysite"
$list = $web.lists["Monster List"]
$cquery = New-Object Microsoft.SharePoint.SPQuery
$cquery.ViewAttributes = "Scope='Recursive'"
$cquery.RowLimit = 1000
$cquery.ViewFields = ""
$cquery.ViewFieldsOnly = $true
do
{
$listItems = $list.GetItems($cquery)
$cquery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
Write-Host "Deleting Monster Item - $($item.Id)"
$list.GetItemById($item.Id).delete()
}
}
while ($cquery.ListItemCollectionPosition -ne $null)

Happy SharePointing Folks!

Leave a Reply

Your email address will not be published.