Delete SharePoint Site using Rest API in SharePoint Online Office 365

SharePoint online delete site using rest api

In this post, we will discuss how we can delete a SharePoint site using Rest API in SharePoint Online Office 365. The same code will also work in SharePoint 2013 and SharePoint 2016.

Also, check:

Create SharePoint Site using Rest API in SharePoint Online Office 365

Here in this example, we are going to take a textbox and a button. The user can put the site title in the textbox and can click on the button to delete the site. The HTML code looks like below and the div with id divResults will display the successful message when the site will get deleted successfully.

Also, read:

Working with Microsoft Flow in SharePoint Online Office 365 and Demo on Send a customized email when a new SharePoint list item is added

HTML Code:

<div id=”DeleteSite”>

<div>

<strong>Enter Name of Site to Delete:</strong>

<br />

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

</div>

<br />

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

</div>

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

Rest API Code:
Below is the Rest API code, here we have taken the site from the textbox. Let us added the full html and rest api code inside a script editor web part. You can also use content editor web part. For this example let us put inside a script editor web part in a web part page.

Also you can check: Steps to create a result source and use custom result source in content search web part in SharePoint online

<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 () {

deleteSite();

});

}

function deleteSite() {

var siteTitle = $(“#txtSiteTitle”).val();

var siteTitleNoSpaces = siteTitle.replace(/\s/g, “”);

var siteUrl = _spPageContextInfo.webAbsoluteUrl;

var fullUrl = siteUrl + “/” + siteTitleNoSpaces + “/_api/web”;

$.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(“Site deleted successfully !”);

}

function onQueryFailed(sender, args) {

alert(‘Error!’);

}

</script>

Once you save the page, the form will appear like below, where use can put the title and click on the Delete Site button. If the site will get deleted successfully, it will show successful message like below:

SharePoint online delete site using rest api
SharePoint online delete site using rest api

Now if you will check the Site Contents page, you can see the site will not be there.

Rest API to delete site SharePoint 2013
Rest API to delete site SharePoint 2013

Hope this will be helpful.


You May Also like the Following SharePoint Online 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 →