// you’re reading...

#SharePoint

Programmatically Delete all items from a SharePoint List

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);
}
}
Programmatically Delete all items from a SharePoint List

You might be interested in:

Discussion

9 comments for “Programmatically Delete all items from a SharePoint List”

  1. Posted by sara | October 5, 2009, 10:22 pm
  2. hi,
    i need to delete list from sharepoint using asp.net gridview.please help me if possible.

    Posted by pankaj | September 29, 2011, 7:39 am
  3. Posted by Sandeep | September 29, 2011, 11:26 pm
  4. Thanks a lot.Though i m already done with it.will try this too.

    Posted by pankaj | October 20, 2011, 7:08 am
  5. Hello i want to upadate all the items of a column.I need to apply one formula for upadating the items.suppose column SUM need to be upadated based on other value of particular item.like A is one column and B is one Column the sum column should be upadated based on A and B

    Posted by sanjeev | November 18, 2011, 12:44 am
  6. thanks alot it helped me 🙂

    Posted by Mohammed Al-Shobani | March 1, 2012, 6:27 am
  7. Here is the solution for deleting items in list with code optimization also.

    http://sasikumarreddyv.blogspot.in/2012/02/how-to-add-list-field-to-dropdownlist.html

    Posted by sasi kumar reddy | March 1, 2012, 9:53 am
  8. @Mohammed Al-Shobani – Glad that it was help to you.

    -Praveen

    Posted by Praveen Modi | March 1, 2012, 9:57 am
  9. Sasi Reddy – Thanks for sharing the link to your blog.

    I checked your code and seems that its for deleting the list and not for purging the list items. I’m missing something?

    -Praveen

    Posted by Praveen Modi | March 1, 2012, 10:01 am

Post a comment

AWS Certified Solutions Architect
    Locations of visitors to this page
View Praveen Modi's profile on LinkedIn

Recent Comments


    Warning: Use of undefined constant comments - assumed 'comments' (this will throw an Error in a future version of PHP) in /home/customer/www/praveenmodi.com/public_html/wp-content/plugins/get-recent-comments/get-recent-comments.php on line 928

    Warning: Use of undefined constant trackbacks - assumed 'trackbacks' (this will throw an Error in a future version of PHP) in /home/customer/www/praveenmodi.com/public_html/wp-content/plugins/get-recent-comments/get-recent-comments.php on line 929