Home > Development, SharePoint > ProcessBatchData To Delete Files from a Document Library

ProcessBatchData To Delete Files from a Document Library

This evening I had to clear down some test data from a large document library. As performance of the delete function degrades on extremely large document libraries (see Software Boundary Document) using an iterative delete function was very slow. In the past I’ve used the SharePoint List web service (http://_vti_bin/lists.asmx) to batch delete files, but I wondered if there was a way to improve performance of the delete through API.

During my investigation I came across an article by The Kid which shows you how to delete large numbers of list items using the ProcessBatchData. I implemented the same code, but the files were not being deleted. Further investigation led me to a great article by SteveC titled SPWeb.ProcessBatchData. A list is a list is a list? Here Steve explains how to delete documents using the ProcessBatchData method and the owsfileref property. The owsfileref is a little known property that takes the ServerRelativeUrl of the file.

Here is the code for my console app that deletes the files in a Batch Method approach. I found it to be extremely quick way of deleting large number of documents from a library.

Hope this helps you it certainly helped me! Saved me loads of time hence why I’m writing as I’m sure I’ll be using it again and again :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace DeleteItemsFromDocumentLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            string SiteUrl = args[0];
            string ListName = args[1];
            int NumberOfFilesToDelete = Convert.ToInt32(args[2]);

            using (SPSite site = new SPSite(SiteUrl))
            {

                using (SPWeb web = site.OpenWeb())
                {
                    SPList CurrentList = web.Lists[ListName];
                    StringBuilder sbDelete = new StringBuilder();
                    sbDelete.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);

                    int i = 0;
                    foreach (SPListItem item in CurrentList.Items)
                    {
                        SPFile file = item.File;
                        if (i >= NumberOfFilesToDelete)
                            break;
                        sbDelete.Append(“<Method>”);
                        sbDelete.Append(“<SetList Scope=\”Request\”>” + CurrentList.ID + “</SetList>”);
                        sbDelete.Append(“<SetVar Name=\”ID\”>” + Convert.ToString(item.ID) + “</SetVar>”);
                        sbDelete.Append(“<SetVar Name=\”owsfileref\”>” + file.ServerRelativeUrl + “</SetVar>”);
                        sbDelete.Append(“<SetVar Name=\”Cmd\”>Delete</SetVar>”);
                        sbDelete.Append(“</Method>”);
                        i++;
                    }

                    sbDelete.Append(“</Batch>”);

                    try
                    {
                       web.ProcessBatchData(sbDelete.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(“Delete failed: ” + ex.Message);
                        throw;
                    }

                    Console.WriteLine(“Deleted {0} files successfully”, NumberOfFilesToDelete);
                    Console.ReadLine();
                }
            }
        }
    }
}

  1. No comments yet.
  1. No trackbacks yet.