Background

Have you ever wanted to delete a SharePoint Online site without using the GUI? Well, this is your lucky day because this blog shows how easy it is to do just that. Before you know it, you’ll be deleting SharePoint Online sites using PowerShell.

Results

For our example, let’s use this test site that needs to be deleted – https://domain-my.sharepoint.com/personal/user_domain_com/test/SitePages/Home.aspx.

Execute the Remove SharePoint Online Site script and the site will be removed.

Before you execute the Remove SharePoint Online Site script, be sure to go through the following steps.

Script



Add-Type
-Path
"c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type
-Path
"c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$siteUrl
=
"https://domain-my.sharepoint.com/personal/user_domain_com/test"

$username
=
"admin@domain.com"

$password
=
Read-Host
-Prompt
"Enter password"
-AsSecureString

$ctx
=
New-Object
Microsoft.SharePoint.Client.ClientContext($siteUrl)

$credentials
=
New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

Credentials = $credentials

$web
=
$ctx.Web

$ctx.Load($web)

$ctx.ExecuteQuery()

$web.DeleteObject()

$ctx.ExecuteQuery()

Write-Host
$web.Title "Site Deleted"