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.