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


Sathish Nadarajan
SharePoint MVP
Published On :   07 Sep 2018
Visit Count
Today :  1    Total :   817




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.

Categories