Delete data from large lists using powershell
cls
$web = Get-Spweb "http://........:/";
$list = $web.Lists["NintexWorkflowHistory"]
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000;
$query.Query = '<Where><Lt><FieldRef Name="ID"/><Value Type="Counter">1000000</Value></Lt></Where>';
Write-Host $list.Title
$itemCount = 0;
$listId = $list.ID;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach ($item in $listItems)
{
Write-Host $item.ID
if($item -ne $null){$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null;$itemCount++;}
}
}
while($query.ListItemCollectionPosition -ne $null)
$batchXml.Append("</Batch>");
$itemCount;
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;
#$web.RecycleBin.DeleteAll()
$web.Dispose()
cls
$web = Get-Spweb "http://........:/";
$list = $web.Lists["NintexWorkflowHistory"]
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000;
$query.Query = '<Where><Lt><FieldRef Name="ID"/><Value Type="Counter">1000000</Value></Lt></Where>';
Write-Host $list.Title
$itemCount = 0;
$listId = $list.ID;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach ($item in $listItems)
{
Write-Host $item.ID
if($item -ne $null){$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null;$itemCount++;}
}
}
while($query.ListItemCollectionPosition -ne $null)
$batchXml.Append("</Batch>");
$itemCount;
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;
#$web.RecycleBin.DeleteAll()
$web.Dispose()
No comments:
Post a Comment