If you want to delete all items in a list or empty / truncate the list by using c# you might be interested in this small script:
[sourcecode language=”csharp”]
SPList targetList = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, “/Lists/YourList”));
SPList delList = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, “/Lists/YourList”));
//If list is empty, do nothing, otherwise delete all items
if (targetList.ItemCount > 0)
{
foreach(SPListItem delItem in targetList.Items)
{
delList.Items.DeleteItemById(delItem.ID);
}
}
[/sourcecode]
The trick is that we get our list two times. First time to iterate through the list, second time to delete the list item. That prevents errors like Index out of range, etc.
..:: I LIKE SHAREPOINT ::..