SharePoint 2010 + PowerShell: Delete Site Content Type

So first, you have to delete all of the content type’s children or you’ll receive the error “Error: content type in use.” Once the dependencies are gone, you can delete the custom site content type itself.

$sitename = $args[0]
$contentType = $args[1]

$web = Get-SPWeb $sitename
$ct = $web.ContentTypes[$contentType]

if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
      foreach ($ctuse in $ctusage) {
        $list = $web.GetList($ctuse.Url)
        $contentTypeCollection = $list.ContentTypes;
        $contentTypeCollection.Delete($contentTypeCollection[$ContentType].Id);
        Write-host "Deleted $contentType content type from $ctuse.Url"
        }
$ct.Delete()
Write-host "Deleted $contentType from site."

} else { Write-host "Nothing to delete." }

$web.Dispose()

 
Viola.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell, SharePoint
4 comments on “SharePoint 2010 + PowerShell: Delete Site Content Type
  1. A Pedant says:

    Viola is a stringed instrument, I think you meant "Voila!"!!

  2. Jesse says:

    I"m running the code but get errors on $sitename on 3rd line, do i add the -url or how do i add my sitename? I tried different methods, but nothing works. Any help will be appreciated.

    Thank you

  3. Mathew says:

    @ Jesse: You can also "hard code" the name of your site, instead of passing it as an argument.
    There are also other ways to retrieve the site you want, which is well documented.
    The more important coding parts in this post, is after the retrieval of the site.

Leave a Reply

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

*