How to delete list attachments using javascript? I have searched for same thing, but so many results are coming in which they are suggesting by using client object model using C# code. I didn't find any javascript code to delete. I have observed in the list item while editing, there is one delete button. If I mouse over on that, it is showing javascript code like javascript:RemoveAttachmentFromServer('{EA96B896-B863-4EF5-85CF-8121B734F3F7}',1). Here the id what is presented not even list id. I think this is attachment id may be, if it is attachment id then how to get this? I have tried with SPServices to get the attachments, but id is not coming for attachments, it is only returning url of the attachment. Is there any way to utilize this function to delete the list attachment?



var listTitle = 'Tasks'
var itemId = 1;
var fileName = 'Instruction.docx';

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var item = list.getItemById(itemId);
var attachmentFile = item.get_attachmentFiles().getByFileName(fileName); 
     console.log('Attachment file has been deleted');  


function deleteAttachmentFile(webUrl,listTitle,itemId,fileName)
   return $.ajax({
      url: webUrl + "/_api/lists/getByTitle('" +  listTitle + "')/getItemById(" + itemId + ")/AttachmentFiles/getByFileName('" + fileName + "')",
      method: 'POST',
      contentType: 'application/json;odata=verbose',
      headers: {
        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
        'X-HTTP-Method' : 'DELETE',
        'Accept': 'application/json;odata=verbose'


     console.log('Attachment file has been deleted');

I have done it using SPServices and it works fine. Below is the code:

    operation: "DeleteAttachment",
    listName: "List Name",
    listItemID: itemId, //list item id
    url: filePath, //url of attachment that needs to be deleted
    completefunc: function (xData, Status) {


you can first fetch all the attachments of a list item and then delete. You need list item id for this. To fetch all attachments you can use the below code:

    operation: "GetAttachmentCollection",
    listName: "ListName",
    ID: itemId, //list item id
    completefunc: function (xData, Status) {
        $(xData.responseXML).find("Attachments > Attachment").each(function (i, el) {
            var $node = $(this),
            filePath = $node.text(),
            //now call DeleteAttachment using this filepath and item id to delete  
  • While deleting it is giving error like: Parameter url is missing – Mihir Apr 17 '15 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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