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);
        item.Delete();
    }

    list.Update();
}

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


One thought on “SharePoint: SPList.Clear() extension method

Leave a Reply

Your email address will not be published. Required fields are marked *