Quantcast
Channel: Mavention
Viewing all articles
Browse latest Browse all 715

Delete items in batch from sharepoint list with powershell script

$
0
0

At Mavention we have a customer with a public facing website at SharePoint 2013 that uses our query suggestions solution for the search box. So, every unique query that visitors search for is added to the query suggestions list. If visitors use a query word or sentence more than once the query count number is increased. All unique queries have to be approved by administrators so they can be used at the website.

But…during 1.5 years they did not once do some administration on this list so it had grown and grown and grown to approximately 140k items. Now they want to delete all items from the list where the “query count” is less than 5. This is approximately 80% of all items.

Well, that is a hell of a job to delete manually. I tried it. But you can only delete 100 item per delete action. So I searched for some batch processing PS script and finally my colleague Waldek Mastykarz (visit his blog) came up with some urls where I could find some info. I used the script from this site: http://matthewyarlett.blogspot.nl/2013/07/well-that-was-fun-bulk-deleting-items.html

The script below contains a query to select all items where the Query Count is < 5. Run it in these 3 stages:

Create the query:

Build the command:

$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)
    {
        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

Run the query:

$web = Get-Spweb http://www.company.com
$web.ProcessBatchData($batchXml.ToString()) | Out-Null


Viewing all articles
Browse latest Browse all 715

Trending Articles