Delete Site Collections with their content DB, Be careful with this!

This script will prompt you for your site collection url, based on your site collection it will find the content DB and present all site collection within the content DB and then will prompt you again if you want to delete all site collection within this content DB, once you enter Yes Please it will start site collection deletions ( please note this can take a while for each site collection ) at the end of each deletion, you will get a notification of the site deleted.

At the end of the site collection deletion the script will ask if you really want to delete the content DB ( this is a permanent deletion !!!). you will have to provide (or copy paste) the content DB name.

 


#site collection url
clear
$SiteCollectionUrl = "http://sitedomain/site" # for your reference copy paste
$UserInputSiteCollection = Read-Host -Prompt 'please provide your site collection url'
$SiteObj = Get-SPSite -Identity $UserInputSiteCollection
$SiteStoragGB = [math]::Round(($SiteObj.Usage.Storage/1024/1024/1024),2);
Write-Host "Site Storage size : " $SiteStoragGB " GB"
$siteContentDB = Get-SPContentDatabase -Identity $SiteObj.ContentDatabase.name
Write-Host "ContentDB Name : " $SiteObj.ContentDatabase.name
Write-Host "ContentDB Site Count : " $SiteObj.ContentDatabase.CurrentSiteCount
$counter = 0;
foreach ($siteObj2 in $siteContentDB.Sites){
  $counter++;
   Write-Host $counter " - " $siteObj2.url
}
#find how many site collections within the content DB, you will need to type Yes Please
$confirmation = Read-Host "Are you sure you want to delete the above site collections? [Yes Please]"
if ($confirmation -eq "Yes Please"){
    foreach ($siteObj2 in $siteContentDB.Sites){
        $counter++;
        Remove-SPSite -Identity $siteObj2.url
        Write-Host $counter " - " $siteObj2.url " !!! Deleted !!!"
   }
}
$confirmation = Read-Host "Are you sure you want to delete this content DB " $siteContentDB.Name "? [Type the content db name]"
if ($confirmation -eq $siteContentDB.Name ){
Remove-SPContentDatabase -Identity $siteContentDB.Name
Write-Host " The following content DB was removed " $siteContentDB.Name " !!! Deleted !!!"
}
IISreset

Leave a Reply

Your email address will not be published. Required fields are marked *