Sometimes you need a quick way to delete all items from a SharePoint list. You can manually delete each item from the list without writing code, but this can be time-consuming, especially if you have a high quantity of items on the list.
Click the link to download the source code for this post
Licensing and Warranty
You may use the code as you wish - it may be used in commercial or other applications, and it may be redistributed and modified. The code is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. By downloading the code, you agree to defend, indemnify, and hold harmless the Author and the Publisher from and against any claims, suits, losses, damages, liabilities, costs, and expenses (including reasonable legal or attorneys' fees) resulting from or relating to any use of the code by you.
You can write code to programmatically remote items from a SharePoint list using the classes in the Microsoft.SharePoint library (namespace). There are two ways that you can delete all the items from the SharePoint list programmatically:
- Using the DeleteItemById() function of the SPList class
- Recommended for performance:Building CAML and using the ProcessBatchData() function of the SPSite class
I will demonstrate both approaches. The skill sets required for these approaches are:
- C# knowledge
- A general knowledge of the classes in the Microsoft.SharePoint library/namespace
- A general knowledge of CAML (for approach #2)
Note: This code will only work on a machine on which SharePoint is installed.
Approach #1: Using the DeleteItemById() function of the SPList class
The function DeleteItemById() of the SPList class expects a numeric parameter ID, which is the ID of the item to delete. This code snippet, which is a slightly modified version of a function in Keith Richie's SPPurgeList (http://www.codeplex.com/sppurgelist), demonstrates how to delete all items by capturing the IDs in a hashtable, reading the hashtable, and calling the DeleteItemById() function. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.
Sample: Using DeleteItemById() [Opens In Another Window]
The issue with Approach #1 is performance. While it is negligible on a list with a small amount of item, it becomes more noticeable on a list with hundreds of items. Each call to DeleteItemById() takes about 1 second. If you have hundreds of items, this could take a while to run.
Approach #2: Building CAML and using the ProcessBatchData() function of the SPSite class
Passing CAML to the ProcessBatchData() function of the SPWeb class runs faster than caling the DeleteItemById(). The following code snippet demonstrates how to build the CAML (using the StringBuilder) and pass the CAML to the ProcessBatchData() function to delete the items. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.
Sample: Using CAML and ProcessBatchData() [Opens In Another Window]
A Note About Using CAML to Delete Items from a Document Library
The code snippet above will work for the lists except for document libraries. If you try to run the code snippet on a document library, you will get an error about file names. The document libraries require an additional parameter called owsfileref. The following link to the code snippet demonstrates how to build the CAML for document libraries.
Sample: CAML for document libraries [Opens In Another Window]
If you have any questions or comments, please feel free to post them, and I will answer the questions to the best of my ability.
2 comments:
Hi,
I enjoyed way you explain this how to delete all items from a list.
Thanks
Vivek
How can this be modified to add items to a list? I have been trying to figure this out so I can populate a list with names & various columns for testing. Then blow it away for production. Your work here is very helpful & appreciated.
Patrick
Post a Comment