SharePoint: SPList.Clear() extension method

Currently there is no good method in SharePoint for deleting all items from list. But there are solutions that need this kind of functionality. During one of my projects I wrote extension method for SPList that removes all items from it.

C#
public static void Clear(this SPList list)
{
    var count = list.ItemCount;
 
    for (var i = count - 1; i >= 0; i--)
    {
        var item = list.GetItemById(i);
        item.Delete();
    }
 
    list.Update();
}

VB.NET


<System.Runtime.CompilerServices.Extension> _
Public Shared Sub Clear(ByVal list As SPList)
    Dim count = list.ItemCount
    
    For i = count - 1 To 0 Step -1
        Dim item = list.GetItemById(i)
        item.Delete()
    Next
    
    list.Update()
End Sub

Feel free to use this code. Also let me know if I can make this code better somehow.

12 Comments

  • I believe that code may fail at some point. Specifically if you create 5 items, then delete one of them (3) but then add a sixth.

    In this case ids are 1, 2, 4, 5, 6. In this case the count is 5 however when you GetItemById(3) it will be null and null.Delete() will fail.

  • I agree with Brad. If you changed it to:

    var item = List.Items[i]

    It should work or possibly a foreach through the items collection but that could have issues while deleting items.

  • This will not work on large libraries. Have a look over here: http://www.blogaboutsharepoint.com/2009/03/16/bulk-deletion-of-splistitems-splistitemcollection

    Its a nice post about bulk deletion.

    Cheers,
    Wes

  • Thanks for feedback, guys. I really appreciate it! :)

    I used my code on list that is filled and emptied through code. I plan to write some more versions of this method for different scenarios.

    It is more correct to use Items collection of list but it performs many times slower when doing operations on it. Also Items collection wants more memory.

    I think I write the next version of this method using ID query so I can avoid loading all items to server memory.

  • where to paste this code?

  • privi, you have to create static class (by example: static class ListExtensions) and put this method in this class. You can call this method later like this:

    var list = web.GetListByUrl(yourListUrl);
    list.Clear();

  • That should work correctly:

    SPListItemCollection ListItemColl = list.Items;

    foreach (SPListItem item in ListItemColl)
    {
    list.GetItemById(item.ID).Delete();
    }
    list.Update();

  • As far as I know there's no reason to call list.Update(), every object is deleted for real when calling SPListItem.Delete().

  • Thanks for your feedback Jonas :)

  • Here list.Update() is required. otherwise we will get error msg like "Item not found".

  • This function ensure you delete all item and ensure that during the delete process if other item are added to the list they also get deleted.

    Public static void deletedAllListItems(this SPList list)
    {
    while(list.ItemCount > 0)
    {
    list.Items.Delete(0);
    }

    list.Update();
    }

  • @PaMa thank you very much this works:



    That should work correctly:

    SPListItemCollection ListItemColl = list.Items;

    foreach (SPListItem item in ListItemColl)

    {

    list.GetItemById(item.ID).Delete();

    }

    list.Update();

Comments have been disabled for this content.