Unable to delete content types


When you try to delete a content type in a farm you sometimes get an error message saying that the Content Type is still in use. You think it is lying, as you have deleted all the lists. Sometimes the reason is because the lists are still sitting in the recycle bin and are still being referenced.

However when you have exhausted a manual look of your site for the rogue list, or item that is still referencing the content type try this bit of code that I found directly on the Microsoft site http://msdn.microsoft.com/en-us/library/ms453791.aspx

Using a console application, the key class to use is SPContentTypeUsage. The below code can easily re-written to allow user prompts for Site, and Content Type, to make it more generic without having to re-compile each time.


namespace ContentTypeInUse
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("http://TestSite"))
            {
                using (SPWeb webSite = siteCollection.OpenWeb())
                {
                    // Get the obsolete content type.
                    SPContentType obsolete = webSite.ContentTypes["DemoCT"];

                    if (obsolete != null) // We have a content type.
                    {
                        IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);
                        if (usages.Count > 0) // It is in use.
                        {
                            Console.WriteLine("The content type is in use in the following locations:");
                            foreach (SPContentTypeUsage usage in usages)
                                Console.WriteLine(usage.Url);
                        }
                        else // The content type is not in use.
                        {
                            // Delete it.
                            Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                            webSite.ContentTypes.Delete(obsolete.Id);
                        }
                    }
                    else // No content type found.
                    {
                        Console.WriteLine("The content type does not exist in this site collection.");
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Advertisements

2 thoughts on “Unable to delete content types

  1. I’ve tried that, unfortunately without success.
    Even if usages.Count > 0 is false, there’s no ability to delete content type.

    • Hi,
      Thank you for your comment. In your case what happens? If usages.Count > 0 is false then the code tries to delete the content type. Does it throw an error message for you? Try putting a try catch block around that ‘if’ statement, the error message might give you some clue. My last thought is maybe there is a child content type, and that is why you cannot delete the given content type, as the code doesn’t check this.

      Hope this helps.

Comments are closed.