How to delete file from document library using JavaScript object model jsom in SharePoint Online Office 365?

delete file from document using jsom sharepoint 2013

In this post, we will discuss how we can delete a file from a document library using the JavaScript object model (jsom) in SharePoint online Office 365. The same code also works in SharePoint 2013 and SharePoint 2016.

Here in this example let us take an input textbox and a button, where the user can put the file name to delete and click on the Delete File button to delete the file from the document library.

SharePoint deveopment training course

Also, you can check some posts on:
Steps to create a file using javascript object model in SharePoint Online Office 365

Configure On-Premise environment for Apps in SharePoint 2016

Configure workflow manager for SharePoint 2016 workflow in windows server 2012 r2

Here we will put the JavaScript and the HTML code inside a script editor web part which we will put inside a web part page. And in this example, we are searching the file inside the Documents document library.

HTML Code:

<div id=”DeleteFile”>


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

<br />

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


<br />

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


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

Jsom Code:

<script src=””></script>


$(function () {



function bindButtonClick() {

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




function deleteDocument() {

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

var clientContext = new SP.ClientContext();

var oWebsite = clientContext.get_web();

var fileUrl = _spPageContextInfo.webServerRelativeUrl + “/Shared Documents/” + docTitle;

this.fileToDelete = oWebsite.getFileByServerRelativeUrl(fileUrl);



Function.createDelegate(this, this.onQuerySucceeded),

Function.createDelegate(this, this.onQueryFailed)



function onQuerySucceeded() {

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


function onQueryFailed(sender, args) {

alert(‘Request failed. ‘ + args.get_message() +

‘\n’ + args.get_stackTrace());



Once you Save the code, you can see a page like below where user can give the file name and then click on the button and on successful deletion it will display a message like below:

sharepoint online file from document using JavaScript object model jsom
sharepoint online file from document using JavaScript object model jsom

After this if you will go to the document library, you can not find the document becuase it has been already deleted.

delete file from document using jsom sharepoint 2013
delete file from document using jsom sharepoint 2013

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


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

View all posts by Bijay Kumar →