JSOM
var listTitle = 'Tasks'
var itemId = 1;
var fileName = 'SharePoint User Guide'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);
attachmentFile.deleteObject();
ctx.executeQueryAsync(
function(){
console.log('Attachment file has been deleted');
},
function(sender,args)
{
console.log(args.get_message());
});
REST
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'
}
});
}
Usage
deleteAttachmentFile(_spPageContextInfo.webAbsoluteUrl,'Tasks',1,'Instru ction'Instruction.docx')
.done(function(data)
{
console.log('Attachment file has been deleted');
})
.fail(
function(error){
console.log(error.responseText);
});