Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.






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.

public static void Clear(this SPList list)
    var count = list.ItemCount;
    for (var i = count - 1; i >= 0; i--)
        var item = list.GetItemById(i);


<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)
End Sub

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

Posted: May 22 2009, 02:46 PM by DigiMortal | with 16 comment(s)
Filed under:


Brad said:

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.

# May 22, 2009 8:45 AM

James said:

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.

# May 23, 2009 11:20 AM

webbes said:

This will not work on large libraries. Have a look over here:

Its a nice post about bulk deletion.



# May 24, 2009 7:50 AM

DigiMortal said:

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.

# May 24, 2009 9:14 AM

privi said:

where to paste this code?

# June 18, 2009 2:23 AM

DigiMortal said:

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);


# June 18, 2009 6:46 AM

PaMa said:

That should work correctly:

SPListItemCollection ListItemColl = list.Items;

foreach (SPListItem item in ListItemColl)





# July 15, 2009 7:33 AM

Jonas said:

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

# July 29, 2009 5:53 AM

DigiMortal said:

Thanks for your feedback Jonas :)

# July 29, 2009 6:09 AM

Eugine said:

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

# November 4, 2010 7:30 AM

Martin Poirier said:

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)






# April 13, 2011 8:20 AM

Ali said:

@PaMa thank you very much this works:

That should work correctly:

SPListItemCollection ListItemColl = list.Items;

foreach (SPListItem item in ListItemColl)





# December 4, 2012 4:00 AM