Deleting Content Types

Last modified: November 01, 2010

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You cannot delete a site content type if it is being used as the basis for other site or list content types. You must first remove this content type from all lists that use it and delete all child site content types that are based on it.

You cannot delete a content type from a list if that list contains items of that content type. However, Microsoft SharePoint Foundation does not consider items that are in the Recycle Bin. If those items are restored after their content type is deleted from the list, those items are assigned the default content type for that list.

To remove a content type from the collection of content types on a list or document library, you first access the collection in the ContentTypes property of the SPList object (server) or the List object (client). Then call the Delete method, passing an SPContentTypeId (server) structure or the ContentTypeId (client) structure that identifies the content type that you want to delete.

To remove a content type from a site collection, access the collection in the ContentTypes property of the SPWeb object (server) or the Web object (client). Then call the Delete method.

Caution note Caution

Both the SPWeb object (server) and Web object (client) have an AvailableContentTypes property that returns a content type collection. This collection is read-only. You cannot delete objects from it. That is because the collection contains all content types that are available in the current site, not only those that are defined in the current site.

In both cases, you must be aware that you cannot delete a content type that is being used. If you are trying to remove a content type from a list, you must first ensure that no list items use the content type. One way to do that would be to iterate through the items in the list and look at the value of the ContentType property of each item. If you are trying to remove a content type from the site collection where it is defined, you must be sure that the GetUsages method returns an empty list—that is, that the content type is not used on any list and is not the parent of any child content type.

The following example shows a console application that verifies whether an obsolete content type is being used in the current website or any child sites. If the content type is not being used, the application deletes it.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

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

               // We have a content type.
               if (obsolete != null) 
               {
                  IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);

                  // It is in use.
                  if (usages.Count > 0) 
                  {
                     Console.WriteLine("The content type is in use in the following locations:");
                     foreach (SPContentTypeUsage usage in usages)
                        Console.WriteLine(usage.Url);
                  }

                  // The content type is not in use.
                  else 
                  {

                     // Delete it.
                     Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                     webSite.ContentTypes.Delete(obsolete.Id);
                  }
               }

               // No content type found.
               else 
               {
                  Console.WriteLine("The content type does not exist in this site collection.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Show: