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 Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. 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 *

*