SharePoint: Bulk delete items in SharePoint Document Library

I had a following code snippet which deleted items in a SharePoint list using Batch Delete method.
private static StringBuilder BatchCommand(SPList spList)
{
    StringBuilder deletebuilder = new StringBuilder();
    deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
    string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
        "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

    foreach (SPListItem item in spList.Items)
    {
        deletebuilder.Append(string.Format(command, item.ID.ToString()));
    }
    deletebuilder.Append("</Batch>");
    return deletebuilder;
}
The above method was called from the main function like this:
using (SPSite spSite = new SPSite(site))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
        StringBuilder deletebuilder = BatchCommand(spWeb.Lists[list]);

        spSite.RootWeb.ProcessBatchData(deletebuilder.ToString());
    }
}
But to my surprise it was not deleting any item in the document library. In order to make sure that the above code is correct I ran it across a custom list and it worked well. After some searching I found that owsfileref also needs to be added in the xml request.

<SetVar Name=\"owsfileref\">{1}</SetVar>
Here is the modified code for bulk deleting items in Document Library:
private static StringBuilder BatchCommand(SPList spList)
{
    StringBuilder deletebuilder = new StringBuilder();
    deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
    string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
        "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"owsfileref\">{1}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

    foreach (SPListItem item in spList.Items)
    {
        deletebuilder.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
    }
    deletebuilder.Append("</Batch>");
    return deletebuilder;
}
  
profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+