Remove Site Collection

07/01/2012 00:34

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction 'SilentlyContinue'
Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.


function Uninstall-AllSPSolutions {
    param (
        [switch] $Local,
        [switch] $Confirm
    )

  Start-SPAssignment -Global;
  foreach($solution in (Get-SPSolution | Where-Object { $_.Deployed })) {
    write-host "Uninstalling Solution " $solution.Name;
    if($solution.DeployedWebApplications.Count -gt 0) {
      Uninstall-SPSolution $solution –AllWebApplications -Local:$Local -Confirm:$Confirm;
    } else {
      Uninstall-SPSolution $solution -Local:$Local -Confirm:$Confirm;
    }
    do {
      Start-Sleep 5;
      $solution = Get-SPSolution $solution;
    } while($solution.JobExists -and $solution.Deployed)
  }
  Stop-SPAssignment -Global;
}

function Remove-AllSPSolutions {
    param (
        [switch] $Confirm
    )
    Get-SPSolution | Where-Object { !$_.Deployed } | Remove-SPSolution -Confirm:$Confirm
}

#clear screen
cls

$siteUrl = 'https://localhost'

if($siteUrl -ne $null)
{
    $siteExists = (Get-SPSite $SiteUrl -ErrorAction SilentlyContinue) -ne $null
    if($siteExists)
    {
        Write-Host ''
        Write-Host 'Deleting SiteCollection', $siteUrl, '...'
        Remove-SPSite –Identity $SiteUrl –GradualDelete -Confirm:$false
        Write-Host ''
        Write-Host 'SiteCollection deleted successfully.'
    }
    else
    {
        Write-Host ''
        Write-Host 'SiteCollection', $SiteUrl, 'does not exist!' -foreground DarkRed
    }
}
else
{
    Write-Host ''
    Write-Host 'Parameter SiteUrl is empty!' -foreground DarkRed
}
    
    Write-Host 'Uninstalling all solutions'    
    Uninstall-AllSPSolutions
    
    Write-Host 'Removing all solutions'
    Remove-AllSPSolutions
    
    Write-Host 'Done'
Stop-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.
Remove-PsSnapin Microsoft.SharePoint.Powershell