Use PowerShell to Remove an Orphaned Custom Site Template from SharePoint 2010

A situation cropped up today where a custom Site Template was created by saving a site as a template which was then used and subsequently deactivated and then deleted. Simple enough except that SharePoint insisted that the template was still there and presented it as a choice when creating new sites even though it didn’t exist and would fail if someone clicked on it.

I could have simply gone into Site Settings – Page Layouts and Site Templates and hidden it there but that raises other problems as well as adds ongoing maintenance as it means all new templates would have to be explicitly added in order to be visible. However, even if I did that, it still doesn’t solve the underlying issue of having a corrupt Site Template in the system. A search showed lots of people with the exact same issue and all without any resolutions.

The key thing that unlocked this was realizing that when SharePoint creates a web template, it creates both the template (as a solution) and a hidden feature that represents it. In our case, the solution was gone (even from the Recycle Bin) but the feature lived on in sort of a half zombie state, being both alive and dead at the same time.

I came up with the script below to actually get rid of the web template feature and hope it helps others!

$templateTitle = "TitleOfTemplateToBeZapped"
$site = Get-SPSite
$templates = $site.GetWebTemplates(1033) #lcid, 1033 = U.S. 
$badTemplate = $templates | where { $_.Title -eq $templateTitle }
if ($badTemplate -ne $null) {
    # the hidden feature Id is the first part of the template 
    #    name for custom web templates
    $id = $badTemplate.Name.substring(0, $badTemplate.Name.IndexOf("#"))
    $badId = [System.Guid]($id)
    write-output "Template '$templateTitle' has been removed"
} else {
    write-output "No template was found with the title of '$templateTitle'"

3 thoughts on “Use PowerShell to Remove an Orphaned Custom Site Template from SharePoint 2010”

  1. Thanks for sharing this, this is what exactly I wanted. However the feature ID returned is _GLOBAL_ for all my custom site templates. We need to pass exact GUID to $site.Features.Remove($badId) for removing these zombie features. Please suggest. Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s