Delete all list items SharePoint 2013 using JavaScript

X

Privacy & Cookies

This site uses cookies. By continuing, you agree to their use. Learn more, including how to control cookies.

Got It!
Advertisements

Introduction: I got the requirement from my clientwhere I need to delete all list items in one shot. That list was having around 5,47,300 items and I need to clear all the items.

Possible Solutions: There are number of possible solutions to perform this task:

  • List Template: Save my list as a template without having data and recreate the list using the saved template. This way you can create the same empty list having same columns. But If list is having lookup columns, this scenario get failed because list template doesnt maintain the relationship of lookup columns.
  • Custom Code: create console app andput5,47,300 items in for/foreach loop and delete item one by one but this will take a very long time and reduce the performance so this is not a preferable way to achieve this task.
  • Powershell: you can write powershell script to achieve this which is faster than custom code but again that will loop through 5,47,300 items which is not acceptable.

Perfect Solution: SPWeb.ProcessBatchData[]Microsoft has provided a way to perform Batch process using ProcessBatchData which is the method of SPWeb class. So using this you can delete5,47,300 items in one shot. Steps are given below to delete bulk items using Processbatchdata method.

Step 1. Open visual studio and create console app.

Step 2. Add the reference of Microsoft.SharePoint then add using Microsoft.SharePoint; at the top of Program.cs file.

Step 3. write the below code under Main method.

try
{
using [SPSite site = new SPSite[//server/, SPUserToken.SystemAccount]]
{
SPWeb web = site.OpenWeb[];
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[List Name];
SPQuery query = new SPQuery[];
query.Query = ;
SPListItemCollection itemcoll = list.GetItems[query];
StringBuilder sbDelete = new StringBuilder[];
sbDelete.Append[];
if [itemcoll != null]
{
foreach [SPListItem item in itemcoll]
{
sbDelete.Append[];
sbDelete.Append[ + Convert.ToString[item.ParentList.ID] + ];
sbDelete.Append[ + Convert.ToString[item.ID] + ];
sbDelete.Append[Delete];
sbDelete.Append[];
}
}
sbDelete.Append[];
web.ProcessBatchData[sbDelete.ToString[]];
web.AllowUnsafeUpdates = false;
}
}
catch [Exception]
{
throw;
}

//server/: You need to insert the URLof your sharePoint site.

List Name: Name of the list where you want to perform bulk delete operation.

Note: you can use the CAML query to filter items instead of deleting all items. In my example I have deleted all the items.

Advertisements

Related

  • Add, Update, Read and Delete SharePoint List item using JavaScript Object Model [JSOM]
  • May 5, 2017
  • In "All Posts"
  • Add, Update, Read and Delete SharePoint List item using PowerShell
  • May 6, 2017
  • In "All Posts"
  • Update Created, Created By, Modified & Modified By Column of List/Library
  • May 1, 2017
  • In "All Posts"

Video liên quan

Chủ Đề