SharePoint 2013 Search - Clean up inactive Topologies

If you have ever had to change or reconfigure your SharePoint 2013 search you probably don't know that you have not cleaned up inactive topologies.

For those interested in cleaning up their Search Service Application copy and paste the PowerShell script below and open PowerShell ISE as Admin and execute.

NB: AS USUAL ALL POWERSHELL IS EXECUTED AT YOUR OWN RISK AND SHOULD BE TESTED IN A DEV AND TEST ENVIRONMENT FIRST

#---- PowerShell Starts here -----------------------------------------------------------------------------------

#===========================================================================
# Add the SharePoint Snapin
#===========================================================================
 Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#==================================================================================
# Get the Search Service Application
#==================================================================================
 
$ssa = Get-SPEnterpriseSearchServiceApplication

#==================================================================================
# Clean out inactive topologies
#==================================================================================

 $oldTopologies = $ssa.Topologies | Where {$_.State -eq "Inactive"}
 if ($oldTopologies -ne $null)
 {
  Write-Host "Removing old Topologies"
  foreach ($oldTopology in $oldTopologies)
  {
     Write-Host "Deleting "$oldTopology.TopologyId.ToString()
     $oldTopology.Delete()
   }
 }

#----- PowerShell Ends here -------------------------------------------------