1

I was trying to figure out how can i delete more than 200 items at a time from list in SharePoint 2010. I can change the view limit and delete 100 items but its pain to delete from both SharePoint list as well from recycle bin . Is there a easy way to delete the huge number of items from list using jquery or javascript . Since we are not suppose to use VS solutions in our environment. Thanks in advance.

2
  • can you use the powershell?
    – Waqas Sarwar MVP
    Commented Jun 16, 2015 at 14:54
  • Hi waqas no i cannot use but may be farm administrator can use. But i am looking something specific to client side .But could you provide here how can we do with PS script which helps in case anyone with javascript doesn't reply. I can give it a shot.
    – SPLearner
    Commented Jun 16, 2015 at 14:55

2 Answers 2

2

Check this blog

Create the query:

$list =  (Get-Spweb http://devmy101).GetList("http://devmy101/Lists/smarEnteredTerritorialWaters")
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000;
$query.Query = '<Where><Gt><FieldRef Name="Created"/><Value Type="DateTime" IncludeTimeValue="TRUE">2013-07-10T14:20:00Z</Value></Gt></Where>';

Build the command (note the query is limited to returning 2000 items at a time, and uses the ListItemCollectionPosition property to continue retrieving items in batches of 2000 until all the items have been queried. See this MSDN documentation for more info.)

$itemCount = 0;
$listId = $list.ID;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );

do
{
    $listItems = $list.GetItems($query)
    $query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach ($item in $listItems)
    {
        if($item -ne $null){$batchXml.Append([System.String]::Format($command, $item.ID.ToString())) | Out-Null;$itemCount++;}
    }
}
while ($query.ListItemCollectionPosition -ne $null)

$batchXml.Append("</Batch>");
$itemCount;

And lastly (and most importantly!), run the query

$web = Get-Spweb http://inceweb/HKMarineDB;
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;
1
  • thank you waqar do you have any idea on how to do same using jquery or java script? thank you.
    – SPLearner
    Commented Jun 16, 2015 at 15:50
0

Here is the code to bulk delete in Javascript.

var context = null;
var web = null;
function deleteAllItems()
{
    var okToContinue = confirm("Are you sure you want to delete all items in this list?");

if(okToContinue)
{
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    context.load(web);
    var ID = SP.ListOperation.Selection.getSelectedList();
    curList = web.get_lists().getById(ID);
    context.load(curList);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Geq><FieldRef Name='ID' />  <Value Type='Number'>1</Value></Geq></Where></Query></View>");
    this.collListItem = curList.getItems(camlQuery);
    context.load(collListItem);

    context.executeQueryAsync(Function.createDelegate
    (this, this.onSuccessGetList, Function.createDelegate,
    this, this.onFailure));
    }
}

function onSuccessGetList(sender, args)
{
    var listItemEnum = collListItem.getEnumerator();
    var counter = 0;
    var itemArr = new Array();

    while(listItemEnum.moveNext())
    {
        var listItem = listItemEnum.get_current();
        itemArr[counter] = listItem.get_item("ID");
        counter++;
    }
    for(var id in itemArr)
    {
        var spItem = curList.getItemById(itemArr[id]);
        spItem.deleteObject();
    } 
    context.executeQueryAsync(Function.createDelegate
    (this, this.onSuccessClean, Function.createDelegate,
    this, this.onFailure));
    }
    function onSuccessClean(sender, args)
    {
        alert("All items deleted!");
        window.location = window.location.href;
    }
    function onFailure(sender, args)
    { 
        alert("Error");
    }

The above code belongs to this blog post where the function is getting used in a ribbon to delete all items in a SharePoint List.

2
  • hi rakib after looking in to the link it shows there used visual studio and inserting this code in master page. Can i use this script in content editor web part or some how to delete the list items ?
    – SPLearner
    Commented Jun 17, 2015 at 13:57
  • You can certainly use this in content editor web part but I would suggest to use it in a separate JS file and add a reference to that file in the master page.
    – Rakibism
    Commented Jun 17, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.