Document ID value is present in a document library column whose internal name is _dlc_DocId
.
So, you can use CAML query to find out which document has a particular document ID and based on that you can delete that document.
Here, I am assuming a document library named Documents
and document ID something like SOMEUNIQUEID-11-11
The JSOM implementation would be as below:
function deleteFileByDocumentId(documentID){
var ctx = new SP.ClientContext.get_current();
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq>' +
'<FieldRef Name=\'_dlc_DocId\'/><Value Type=\'Text\'>UNIQUEID-11-11</Value>' +
'</Eq></Where></Query></View>');
var items = ctx.get_web().get_lists().getByTitle('Documents').getItems(camlQuery);
ctx.load(items);
ctx.executeQueryAsync(function () {
var itemCount = items.get_count();
for (var i = itemCount - 1; i >= 0; i--) {
var oListItem = items.itemAt(i);
oListItem.deleteObject();
};
clientContext.executeQueryAsync(function(){
console.log("success deleting document");
}, function(){
console.log("something went wrong");
});
},function(){
console.log("something went wrong");
});
}
RESTful implementation would be as below (modified from here):
function getItemsToDelete() {
var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('Documents')/items?$filter=OData__dlc_DocId eq 'SOMEUNIQUEID-11-11'&$select=Title,FileRef";
return $.ajax({
url: requestUrl,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function(result) {
$.each(result.d.results, function(index, item){
itemsToDelete.push(deleteItem(item));
});
},
error: function(error) {
console.log(error);
}
});
}
function deleteItem(item) {
var requestUrl = item.__metadata.uri;
return $.ajax({
url: requestUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE"
},
success: function() {
console.log("File with ID " + item.__metadata.id + " successfully deleted!");
},
error: function(error) {
console.log(error);
}
});
}
var itemsToDelete = [];
$.when(getItemsToDelete()).then(function () {
$.when.apply($, itemsToDelete).then(function(){
console.log("All docs with unique document ID are deleted!");
});
});