Friday, September 30, 2011

Deleting all items from lists

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;
}
}
}
Article: Batch command , SP List Delete

5 comments:

Unknown October 1, 2011 at 5:19 PM  

This console app is quite useful. I have been thinking about writing one like this myself. You saved me all the time :) Thanks for posting this.

Sandeep October 1, 2011 at 11:43 PM  

One more thing this quick app does is to clean all the item from recycle bin..

Nisus October 5, 2011 at 6:18 AM  

also you can switch off RecycleBin during deletion

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);
bool recycleBinEnabledSaved = web.Site.WebApplication.RecycleBinEnabled;
if (deleteFromRecycleBin)
web.Site.WebApplication.RecycleBinEnabled = false;
try
{
web.ProcessBatchData(sbDelete.ToString());
}
finally
{
if (deleteFromRecycleBin)
web.Site.WebApplication.RecycleBinEnabled = recycleBinEnabledSaved;
}

Console.WriteLine("Done cleaning list : Presss a key to continue with next list.");
Console.WriteLine("");
Console.ReadKey();
}
}

Sandeep October 5, 2011 at 11:19 AM  

I will not recommend switching recycle bin off for 2 reasons

1. This code will not work as sandbox solution

2. Other site collection will also get affected by this setting for say 2 min... ( risky in PROD )

baggu,  November 22, 2011 at 4:52 AM  

Hi Sandeep,

A very nice post. I found it very useful for my requirement and it saved my time. good job!!!