How to Delete the Modern Sites Using PowerShell in SharePoint Office 365

Sathish Nadarajan
 
Solution Architect
September 7, 2018
 
Rate this article
 
Views
2853

 

In this article, a small PowerShell Snippet to delete the SiteCollections from Office 365 is explained.

There are two methods used in this script. Which is mandatory. Because, when we delete from the UI or only delete the site, the site will remain in the Recycle Bin. We need to clear the site from the Recycle bin as well. Otherwise, when we try to create again with the same site name, system will thrown an exception.

 

 # Delete the SiteCollection
 
 ##================================================================================================
 ## Description	: To Delete the Sitecollections
  
  
 ## Author		: Sathish
 ## Date			: 05-Apr-2018
 ##================================================================================================
 
 # ============================================ Setup Input Paths =================================
 
 cls
 
  
 $Host.UI.RawUI.WindowTitle = "-- Delete the Site Collection --"
 
 $StartDate = Get-Date
  
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Delete the Site Collection |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
  
 
 ################# Set the Current Path as Execution Path ####################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 ############# set the Error Preference ################
 
 $ErrorActionPreference = "Stop"
 
 
 $adminUrl = Read-Host "Enter the Admin URL of your Tenant"
 
 #connect to the Tenant Admin Site
 Connect-SPOService -Url $adminUrl
 
 try
 {
     #Delete the Site
     $siteURL = Read-Host "Enter the URL of the site to be deleted"
 
 
     #Delete the Site from the Tenant
     Remove-SPOSite -Identity $siteURL -Confirm:$false 
 
     #Delete the Site from the Recycle Bin 
     Remove-SPODeletedSite -Identity $siteURL -Confirm:$false 
 
     Write-Host -ForegroundColor Green $siteURL "Site Deleted Successfully" 
 }
 catch
 {
     cls
     Write-Host -ForegroundColor Red "Exception Occurred" 
     Write-Host -ForegroundColor Red $Error[0].Exception.Message
 }
 

 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment