Invoke-RemoveListItems.ps1
<############################################
Script : Invoke-RemoveListItems Company : ItsZap Inc. Author : Riwut Libinuko Blog : http://blog.libinuko.com Licence : GNU GENERAL PUBLIC LICENSE Copyright © 2016, ItsZap Inc, Riwut Libinuko (cakriwut@gmail.com). All Rights Reserved #############################################> <#PSScriptInfo .VERSION 1.0.2 .GUID 540480a1-a911-402a-9e77-61597c3f3f02 .AUTHOR Riwut Libinuko .COMPANYNAME ItsZap Inc. .COPYRIGHT Riwut Libinuko (cakriwut@gmail.com), ItsZap Inc. All Rights Reserved .TAGS SharePoint, DevOps .LICENSEURI .PROJECTURI https://gist.github.com/cakriwut .ICONURI https://avatars0.githubusercontent.com/u/15277525?v=3&s=200 .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .DESCRIPTION Delete SharePoint List items in batch and provide sleep between 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. #> function Invoke-RemoveListItems{ <# .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.0 Author: Riwut Libinuko Creation Date: 11/11/2016 .EXAMPLE Invoke-RemoveListItems -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() } |