// you’re reading...

SharePoint

Programmatically Delete all items from a SharePoint List

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

At times we want to delete all the items from a SharePoint List. Here are some excellent solutions to delete all the SharePoint list items efficiently.

Below is the PurgeList method from Keith Richie which does an excellent job of Purging items in a list. Here is a CodePlex tool and  one more solution to deal with the same problem.

/// <summary>
/// Purges items and folders from a list
/// Define WSSV3 to remove list folders
/// </summary>
/// The SPList you want to
/// purge items from
private static void PurgeList(SPList list)
{
Console.WriteLine("Purging list: " + list.Title);
Console.WriteLine("Base Type: " + list.BaseType.ToString());

// ===========================================================
// list.ItemCount returns a count that includes all items
// "AND" folders.
// You can't use list.Items.DeleteItemById() to remove a
// folder
// ===========================================================
System.Collections.Hashtable hItems =
new System.Collections.Hashtable(list.ItemCount);

// ===========================================================
// SPList.Items returns all list items in the entire list
// regardless of folder containment
// Note, just because list.ItemCount includes folders,
// list.Items does not.
// ===========================================================
foreach (SPListItem item in list.Items)
hItems.Add(item.ID,null);

// Remove the list items
foreach (int ID in hItems.Keys)
list.Items.DeleteItemById(ID);
// Clear the hashtable
hItems.Clear();
// ===========================================================
// SPList.Folders returns all folder items in the entire list
// regardless of parent folder containment
// ===========================================================
foreach (SPListItem item in list.Folders)
hItems.Add(item.ID,null);

// Remove the folder items
foreach (int ID in hItems.Keys)
{
list.Folders.DeleteItemById(ID);
}
}
You might be interested in:
  • SharePoint 2007 Test Data Population Tool
  • SHAREPOINT DESIGNER 2007 IS NOW FREE!
  • Set the SharePoint Welcome Page through code
  • SharePoint - Add or Delete Site Column reference from Content Types
  • Review of: SharePoint Content Deployment Wizard
  • Discussion

    One comment for “Programmatically Delete all items from a SharePoint List”

    1. Posted by sara | October 5, 2009, 10:22 pm

    Post a comment

    Spam Protection by WP-SpamFree Plugin

      Locations of visitors to this page
    View Praveen Modi's profile on LinkedIn

    Recent Comments

    • fred: The above mentioned tips are helpful. However, by the use of a third-party caching solution for SharePoint you...
    • Ramiz Uddin: Praveen, I would need your help. I’m new on sharepoint and very much confused with its deployment...
    • Maarten: Hi! I tried your code, it works great. However, I don’t see the created folder in explorer view. How...
    • Praveen Modi: Juan, seems like it should work but I haven’t tried it. I will give it a try and will update the...
    • Juan Larios: couldn’t you just do: SPFolderCollection collection = site.Lists["Site Collection...