Last active
August 29, 2020 17:01
-
-
Save cakriwut/3b19fa7befedddb715f8 to your computer and use it in GitHub Desktop.
PowerShell to delete SharePoint list items in batch mode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DeleteListItems{ | |
<# | |
.SYNOPSIS | |
Deletes SharePoint List Items in batch | |
.DESCRIPTION | |
Deleted SharePoint List items in batch and provide sleep between batch. By default, it will delete all List Items. | |
You can delete List Items based on the CAML Query input parameter. | |
.PARAMETER ListUrl | |
Mandatory. SharePoint List Url to be deleted. For example: http://www.contoso.com/Lists/CustomList | |
.PARAMETER BatchSize | |
Optional. Batch Size per deletion request. For example if you specify 2000, and your list contains 10.000 items, then the | |
application will execute 5 deletion request with 2000 items deletion per request. Default: 2000 | |
.PARAMETER CamlQuery | |
Optional. Valid CAML Query to specify deletion criteria. Default: "<Where><Geq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Geq></Where>" | |
.INPUTS | |
Parameters above | |
.OUTPUTS | |
None | |
.NOTES | |
Version: 1.1 | |
Author: Riwut Libinuko | |
Creation Date: 29/12/2015 | |
Last Update: 11/03/2018 | |
.EXAMPLE | |
DeleteListItems -ListUrl http://www.contoso.com/Lists/ListToDelete | |
#> | |
Param ( | |
[Parameter(Mandatory=$true)][string]$listUrl, | |
[int]$batchSize=2000, | |
[string]$camlQuery = "<Where><Geq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Geq></Where>" | |
) | |
$spSite = new-object Microsoft.SharePoint.SPSite($listUrl) | |
$spWeb = $spSite.OpenWeb() | |
$list = $spWeb.GetList($listUrl) | |
Write-host "Intializing SharePoint object for $listUrl" | |
$starttime=get-date | |
Write-host "Deleting items in $listUrl with in $batchSize per batch" | |
$query = New-Object Microsoft.SharePoint.SPQuery | |
$query.ViewAttributes = "Scope='Recursive'" | |
$query.RowLimit = $batchSize | |
$query.Query = $camlQuery | |
$itemCount = 0 | |
$listId = $list.ID | |
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" ) | |
do | |
{ | |
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder" | |
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>") | |
$listItems = $list.GetItems($query) | |
$listItems.ListItemCollectionPosition | |
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition | |
Write-Host "Preparing" -NoNewline | |
if($listItems.Count -gt 0) { | |
foreach ($item in $listItems) | |
{ | |
if($item -ne $null){ | |
$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null; | |
$itemCount++; | |
Write-Host "." -NoNewline | |
} | |
} | |
Write-Host "." | |
$batchXml.Append("</Batch>") | |
Write-Host "Total $($listItems.Count) will be deleted.." | |
Write-host "Sending batch command" | |
$web.ProcessBatchData($batchXml.ToString()) | Out-Null | |
Start-Sleep -Seconds 2 #Need to give some fresh air | |
} | |
} | |
while ($query.ListItemCollectionPosition -ne $null) | |
$endtime=get-date | |
$span=[System.timespan]($endtime - $starttime) | |
$timeSpent=$span.TotalSeconds | |
Write-host "Deleting total $itemCount item(s) in $timeSpent seconds" | |
$spWeb.Dispose() | |
$spSite.Dispose() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great script, I think
$web
at line 81 should be changed to$spWeb