DeleteListItems.ps1
<#PSScriptInfo
.VERSION 1.3.0 .GUID 4d6dbc23-abff-4192-9e44-ebe4586292ec .AUTHOR Riwut Libinuko .COMPANYNAME ItsZap Inc. .COPYRIGHT Copyright (c) 2018, ItsZap Inc, Riwut Libinuko (cakriwut@gmail.com). All Rights Reserved .TAGS SharePoint, DevOps .LICENSEURI .PROJECTURI https://gist.github.com/cakriwut/3b19fa7befedddb715f8 .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .DESCRIPTION Delete SharePoint List Items in batch #> <# .DESCRIPTION Delete SharePoint List Items in batch #> <############################################ Script : DeleteListItems Company : ItsZap Inc. Author : Riwut Libinuko Blog : http://blog.libinuko.com Licence : GNU GENERAL PUBLIC LICENSE Copyright © 2018, ItsZap Inc, Riwut Libinuko (cakriwut@gmail.com). All Rights Reserved #############################################> function DeleteListItems{ <# .SYNOPSIS Delete SharePoint List Items in batch .DESCRIPTION Delete 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: 11/11/2016 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() } |