Scenario:
Often during data migration we need to do clean up list data. I had something similar but there were multiple lists and each list with 5000-10000 records.
Solution:
Quick console application did the trick of cleaning all the lists.
>> Create a new console application and add reference to Microsoft.SharePoint.dll
>> Use the below code.
Code: [ Applies To : WSS3.0 / Sharepoint 2010 Foundation ]
using System; using Microsoft.SharePoint; using System.Collections.Generic; using System.Text; namespace SKN.CleanSPLists { class Program { static void Main (string[] args) { string webUrl = "http://ws2003"; bool deleteFromRecycleBin = true; List<string> listNames = new List<string>(); listNames.Add("List Title 1"); listNames.Add("List Title 2"); listNames.Add("List Title 3"); listNames.Add("List Title 4"); using (SPSite site = new SPSite(webUrl)) { using (SPWeb web = site.OpenWeb()) { foreach (var listName in listNames) { SPList list = web.Lists[listName]; Console.WriteLine("Purging list: " + list.Title); Console.WriteLine("Base Type: " + list.BaseType.ToString()); StringBuilder sbDelete = BuildBatchDeleteCommand(list); web.ProcessBatchData(sbDelete.ToString()); Console.WriteLine("Done cleaning list : Presss a key to continue with next list."); Console.WriteLine(""); Console.ReadKey(); } if (deleteFromRecycleBin) { Console.WriteLine("Cleaning Recycle bin"); web.RecycleBin.DeleteAll(); site.RecycleBin.DeleteAll(); Console.WriteLine("Done cleaning Recycle bin"); Console.ReadKey(); } } } } /// <summary> /// Builds a batch string with a list of all the items that are to be deleted. /// </summary> /// <param name="spList"></param> /// <returns></returns> private static StringBuilder BuildBatchDeleteCommand (SPList spList) { StringBuilder sbDelete = new StringBuilder(); sbDelete.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) { sbDelete.Append(string.Format(command, item.ID.ToString())); } sbDelete.Append("</Batch>"); return sbDelete; } } }