Different Methods To Do Bulk Delete Of SharePoint List items

Recently, I faced an issue in my Team site where the workflow history list consumed 96% of the space and due to insufficient space, my Team site's performance became very poor.

When I tried to open the list, I got the following error. 

“This view cannot be displayed because it exceeds the list view threshold (100000 items) enforced by administrator”.
 
Due to the exceeded threshold limit, I couldn’t check how many items were there to delete. I even tried to create a new view but couldn't see more than 100,000 items as enforced by the administrator.

Then, I decided to use the REST API to get to know the total number of items, so I have used the below-mentioned syntax on the browser.

https://sharepoint.com/_api/webs/lists/GetByTitle(“Workflow History”)/itemcount

Finally, I found there are more than 55 lakh items that need to be deleted. It’s a very huge count really but the problem is I don’t have access to the application server to perform PowerShell script.

So as my first attempt, I planned to create JavaScript button event to vanish the list items in a single click but unfortunately, it deleted only a few items at a time.

  1. <input type = "button" id = "btnSubmit" value = "Delete All Items"/>   
  2. <br/>   
  3. <script src ="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" >   
  4. </script>  
  5. <script >  
  6. $(function() {  
  7.     bindButtonClick();  
  8. });  
  9.   
  10. function bindButtonClick() {  
  11.     $("#btnSubmit").on("click"function() {  
  12.         deleteAllItemsFromList();  
  13.     });  
  14. }  
  15. var clientContext;  
  16. var website;  
  17. var oList;  
  18. var cnt = 0;  
  19.   
  20. function deleteAllItemsFromList() {  
  21.     clientContext = SP.ClientContext.get_current();  
  22.     website = clientContext.get_web();  
  23.     oList = website.get_lists().getByTitle('ListName');  
  24.     var camlQuery = new SP.CamlQuery();  
  25.     camlQuery.set_viewXml('<View><RowLimit>3000</RowLimit></View>');  
  26.     this.collListItem = oList.getItems(camlQuery);  
  27.     clientContext.load(website);  
  28.     clientContext.load(collListItem, 'Include(Id)');  
  29.     clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceeded), Function.createDelegate(thisthis.onQueryFailed));  
  30. }  
  31.   
  32. function onQuerySucceeded(sender, args) {  
  33.     var listItemInfo = '';  
  34.     var listItemEnumerator = collListItem.getEnumerator();  
  35.     while (listItemEnumerator.moveNext()) {  
  36.         var oListItem = listItemEnumerator.get_current();  
  37.         var ID = oListItem.get_id();  
  38.         var oListItemDel = oList.getItemById(ID);  
  39.         oListItemDel.deleteObject();  
  40.         clientContext.executeQueryAsync(Function.createDelegate(thisthis.onDeleteSucceeded), Function.createDelegate(thisthis.onDeleteFailed));  
  41.     }  
  42. }  
  43.   
  44. function onQueryFailed(sender, args) {  
  45.     alert('Failed');  
  46. }  
  47.   
  48. function onDeleteFailed(sender, args) {  
  49.     alert('Failed');  
  50. }  
  51.   
  52. function onDeleteSucceeded(sender, args) {  
  53.     cnt = cnt + 1;  
  54.     var waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('Loading...''', 100, 330);  
  55.     setTimeout("location.reload(true);", 25000);  
  56. }   
  57. </script>    

Then, as a second attempt, I loaded all items to MS Access and performed delete query from there; but it moved all the deleted items to recycle bin.

The following steps had helped me to load the SharePoint list items to MS Access.

Step 1

Choose a blank desktop database.

Step 2

Select external data from the ribbon, click "More", and choose SharePoint list,
 
 
Step 3
 
Enter your site collection URL and choose the link to the data source by creating a linked table.
 
 
Step 4
 
It will check for the credentials and then it will populate all the list and library structure on next pop-up window.

Step 5

Select the respective list and click OK.

Step 6

Once the items are loaded, click "Create" from the ribbon and select Query Design then choose the List.
 
 
Step 7
 
Click view and change the view to SQL Query, then type the query given below and click F5 to execute the query,
 
 
  1. Delete from [SharePoint List] where ID between 1 and 100000  

The above given query will delete the items from ID 1 to 100000, like this you can change the query based on your requirement. As I said earlier this method will move all items to your recycle bin so it will make more complex to delete your recycle bin.

Then as my final attempt, I planned to create one PowerShell script using CSOM method, since I don’t have access to application server. So due to low system configuration I could not delete all 55+ lakhs items at a time so I changed the view as 5000 items using CAML Query and loaded that PowerShell script in my local computer Task scheduler, it will execute the PowerShell script on each 15 minutes to delete the mentioned 5000 items, this method taken nearly 2 days to delete all 55+ Lakhs items from my team site.

  1. Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  2. Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  3. $clientContext = New - Object Microsoft.SharePoint.Client.ClientContext "Https://Sharepoint.com/sites/subiste"  
  4. $listname = "Workflow History"  
  5. Do {  
  6.     $web = $clientContext.Site.RootWeb  
  7.     $clientContext.Load($web)  
  8.     $clientContext.ExecuteQuery()  
  9.     $list = $clientContext.Web.Lists.GetByTitle($listname)  
  10.     $clientContext.Load($list)  
  11.     $clientContext.ExecuteQuery()  
  12.     $query = New - Object Microsoft.SharePoint.Client.CamlQuery  
  13.     $query.ViewXml = ” < View > < RowLimit > 5000 < /RowLimit></View > ”$items = $list.GetItems($query)  
  14.     $clientContext.Load($items)  
  15.     $clientContext.ExecuteQuery()  
  16.     if ($items.Count - gt 0) {  
  17.         for ($i = $items.Count - 1; $i - ge 0; $i– - ) {  
  18.             $items[$i].DeleteObject()  
  19.             write - host $items[$i] deleted + $i  
  20.         }  
  21.         $clientContext.ExecuteQuery()  
  22.         write - host "Operations Completed"  
  23.         Clear - host  
  24.     }  
  25. }  
  26. While($items.Count - eq 0) {  
  27.     Write - Host "All Items are deleted in the List" - ForegroundColor Green