How to delete file using Rest API in SharePoint Online Office 365?

delete file using Rest API in SharePoint Online Office 365
InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can delete file from document library using Rest API in SharePoint Online Office 365. Here in this example we have a file name as Bijay.txt inside the documents document library. We will delete the file using Rest API.

The document library looks like below:

How to delete file using Rest API in SharePoint Online Office 365?
How to delete file using Rest API in SharePoint Online Office 365?

Here we have taken a html input box and a submit button. User will give a name in the textbox and click on the Delete File button which will delete the file from the document library.

Below is the HTML Code:

Here let us add both the html and Rest API code inside a script editor web part inside a web part page.

HTML Code:
<div id=”DeleteFile”>

<div>

<strong>File Name to Delete:</strong>

<br />

<input type=”text” id=”txtDocumentTitle” />

</div>

<br />

<input type=”button” id=”btnSubmit” value=”Delete File” />

</div>

<div id=”divResults”></div>

Below are the Rest API code. Here we are calling the deleteDocument() method on the button click.

Rest API Code:
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

deleteDocument();

});

}

function deleteDocument() {

var docTitle = $(“#txtDocumentTitle”).val() + “.txt”;

var siteUrl = _spPageContextInfo.webAbsoluteUrl;

var webRelUrl = _spPageContextInfo.webServerRelativeUrl;

var fullUrl = siteUrl + “/_api/web/GetFileByServerRelativeUrl(‘” + webRelUrl +”/Shared Documents/” + docTitle +

“‘)”;

$.ajax({

url: fullUrl,

type: “POST”,

headers: {

“accept”: “application/json;odata=verbose”,

“content-type”: “application/json;odata=verbose”,

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),

“X-HTTP-Method”: “DELETE”,

“IF-MATCH”: “*”

},

success: onQuerySucceeded,

error: onQueryFailed

});

}

function onQuerySucceeded() {

$(“#divResults”).html(“Document successfully deleted!”);

}

function onQueryFailed(sender, args) {

alert(‘Error!’);

}

</script>

Once you Save the code and refresh the page, then you can see the page will appear like below, where user can give the file name and click on the Delete File button which will delete the file from the document library.

SharePoint 2013 delete file using rest api
SharePoint 2013 delete file using rest api

Now if we will check the document library the file is not there like below:

delete file using Rest API in SharePoint Online Office 365
delete file using Rest API in SharePoint Online Office 365

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply