SharePoint bulk DELETE function

I had the requirement to delete multiple list items for a clients SharePoint 2013 site. I wanted to stick with using REST as opposed to the CSOM as my custom built framework makes use of it primarily.

The way to accomplish this is to take advantage of ODATA’s batching. Rather than go to much into detail I’ll just give you the function.

The usage is quiet simple:


var batch = [ {  listName: "", id: }, ..... //more here ]

deleteByBatch(batch, success, error);


deleteByBatch: function(batch, success, error) {
        var endpoint = _spPageContextInfo.webAbsoluteUrl + '/_api/$batch';
        // generate a batch boundary
        var batchGuid = generateUUID();
        //creating the body
        var batchContents = [];
        var changeSetId = generateUUID();
        //iterate through each item in batch
        for (var i = 0; i & lt; batch.length; i++) {
            var itemToDelete = batch[i];
            //create the request endpoint
            var endpoint = _spPageContextInfo.webAbsoluteUrl +
                "/_api/web/lists/GetByTitle('" + itemToDelete.listName + "')/items(" + 
                itemToDelete.id + ")";
            // create the changeset
            batchContents.push('--changeset_' + changeSetId);
            batchContents.push('Content-Type: application/http');
            batchContents.push('Content-Transfer-Encoding: binary');
            batchContents.push('');
            batchContents.push('DELETE ' + endpoint + ' HTTP/1.1');
            batchContents.push('IF-Match: *');
            batchContents.push('');
            batchContents.push('');
        }
        // END changeset to create data
        batchContents.push('--changeset_' + changeSetId + '--');
        // batch body
        var batchBody = batchContents.join('\r\n');
        batchContents = [];
        // create batch for creating items
        batchContents.push('--batch_' + batchGuid);
        batchContents.push('Content-Type: multipart/mixed; boundary="changeset_' 
            + changeSetId + '"');
        batchContents.push('Content-Length: ' + batchBody.length);
        batchContents.push('Content-Transfer-Encoding: binary');
        batchContents.push('');
        batchContents.push(batchBody);
        batchContents.push('');
        batchBody = batchContents.join('\r\n');
        // create the request endpoint
        var endpoint = _spPageContextInfo.webAbsoluteUrl + '/_api/$batch';
        var batchRequestHeader = {
            'X-RequestDigest': jQuery("#__REQUESTDIGEST").val(),
            'Content-Type': 'multipart/mixed; boundary="batch_' + batchGuid + '"'
        };
        // create request
        jQuery.ajax({
            url: endpoint,
            type: 'POST',
            headers: batchRequestHeader,
            data: batchBody,
            success: function(response) {
                if (success) {
                    success(response);
                }
            },
            fail: function(msg) {
                console.log("error", msg);
                if (error) {
                    error(msg);
                }
            }
        });

Related Posts

Creating SharePoint Online Remote Event Receiver
New SharePoint 2016 Features: Microsoft Flow App, PowerApps, and Updated Framework
Upsert function for Rest SharePoint API
Provider Hosted App Times Out

Leave a Comment