We have several ways to delete all items from a SharePoint List. I want to place all the code snippets in a single place and i want to highlight the better approach also.
Generally we will use for loop through iterate the list items and call the delete method for each list item. It is very costly operation when we want to delete all the list items.
General Approach : --
Code Snippet : --
Please find the nice explanation for the above code snippet by following this links.
Link 1 Link 2
We can do the same thing using power shell also. Please find the links below.
http://bramnuyts.be/2013/05/16/remove-items-from-a-huge-sharepoint-list/
Happy coding. I hope these code snippets gives a complete idea.
Generally we will use for loop through iterate the list items and call the delete method for each list item. It is very costly operation when we want to delete all the list items.
General Approach : --
using (SPSite oSPsite = newSPSite("http://testsite")) { using (SPWeb oSPWeb = oSPsite.OpenWeb()) { oSPWeb.AllowUnsafeUpdates = true; SPList oList = oSPWeb.TryGetList("SampleList"); if(oList!=null) { SPListItemCollection items = oList.Items; int itemCount = items.Count; for (int i = 0; i < itemCount; i++) { items.Delete(i); } oSPWeb.AllowUnsafeUpdates = false; } } }
Initially i used for each loop to iterate the list items, it will give an error as Collection was modified; enumeration operation may not execute.
Don't use for each loop to iterate the list items when we are iterating the list items. I have found a nice blog post which will give a clear picture when we are deleting all list items from a list.
Please click here to see the blog post.
Now we will discuss a better approach to delete the all items from a SP list. It is small change in for loop. Trick is we need to use decrements the for loop. because items are being deleted and the number of items decreases with each decrements. Code Snippet : --
for (int i = list.Items.Count - 1; i >= 0; i--) { list.Items.Delete(i); }
We have better approach rather than iterating the for each loop either increment or decrements which is Bulk Delete using batch process.Using batch process we can delete the list items as a single batch. Advantage of this approach is single hit to the server to delete all the list items.
void DeleteBulkItems(SPWeb web,SPList list, String Ids) { StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>"); string command = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>"; string[] _DeletedIDs = Ids.Split(','); foreach (String item in _DeletedIDs) { sbDelete.Append(string.Format(command, item.ToString())); } sbDelete.Append("</Batch>"); web.ProcessBatchData(sbDelete.ToString());}
Please find the nice explanation for the above code snippet by following this links.
Link 1 Link 2
We can do the same thing using power shell also. Please find the links below.
http://bramnuyts.be/2013/05/16/remove-items-from-a-huge-sharepoint-list/
Happy coding. I hope these code snippets gives a complete idea.
No comments:
Post a Comment