PowerShell Script to delete orphaned SharePoint SPSites with Force
SharePoint content movement within various environments is one of the frequent tasks for SharePoint Administrators. Site administrators, developers or testers create/update/delete a lot of testing site collections in an existing web application, where you already have hundreds of site collections restored from Production.
In some scenarios, where few of the test sites became orphaned, site collection does not exist in the farm even though content database has an entry. For example, a site collection showing in central admin/ Get-SPSite command but it is not fetching other details or not even giving the option to delete it.
In such a case, the PowerShell commands below are very helpful to clean these.
Please follow below script to forcibly delete these corrupt sites:
- Tried only in SharePoint 2010/2013
- Run in any lower environment first to test this script.
###################################################
## Script to delete currupt sites with Force ##
## ##
## -Run in SharePoint Management Shell ##
## -Change $CorruptSite variable with your ##
## Site collections ##
###################################################
Add-PSSnapin Microsoft.SharePoint.PowerShell
$CorruptSites = "http://rk-spsolutions.com/sites/a","http://rk-spsolutions.com/sites/b","http://rk-spsolutions.com/sites/c"
Function DeleteCorruptSite
{
param($CorruptSite)
Write-Host $CorruptSite : -foreground red
$site = Get-SPSite $CorruptSite
$siteId = $site.Id
$siteDatabase = $site.ContentDatabase
$siteDatabase.ForceDeleteSite($siteId, $false, $false)
Write-Host --Deleted from Content DB -foreground green
}
foreach ($CorruptSite in $CorruptSites)
{
DeleteCorruptSite $CorruptSite
}