Efficiently purge large lists

Sometimes you may need to clear the contents of a huge SharePoint list.

You can do this through the web user interface, but you will probably face some limitations: bulk operations are limited to 100 elements, the folder structure may be an obstacle, etc…

If you prefer writing script code (and if you have remote access to a farm server), you may gain quite a lot of time.

Here’s how:

function ClearList([Microsoft.SharePoint.SPWeb]$web, [Microsoft.SharePoint.SPList]$list)

{

    $sbDelete = new-object System.Text.StringBuilder

    $sbDelete.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>")

    $command = "<Method><SetList>" + $list.ID + "</SetList><SetVar Name=`"ID`">{0}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>";

    $items = $l.Items

    foreach ($item in $items)

    {

        [void]$sbDelete.Append([System.String]::Format($command, $item.ID.ToString()))

    }

    [void]$sbDelete.Append("</Batch>");

    try

    {

        $web.ProcessBatchData($sbDelete.ToString());

    }

    catch [Exception] {

        Write-Host $_.Exception.ToString()

    }   

}

This can be used just for SharePoint list, for document libraries you should use a slightly different approach, anyway…

Hope useful!