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.

Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp

   Sub Main()
      Using siteCollection As SPSite = New SPSite("http://localhost")
         Using webSite As SPWeb = siteCollection.OpenWeb()

            ' Get the obsolete content type.
            Dim obsolete As SPContentType = webSite.ContentTypes("Test")

            ' We have a content type
            If obsolete IsNot Nothing Then 
               Dim usages As IList(Of SPContentTypeUsage) = SPContentTypeUsage.GetUsages(obsolete)
               If usages.Count > 0 Then ' It is in use

                  Console.WriteLine("The content type is in use in the following locations:")
                  For Each usage As SPContentTypeUsage In usages
                  Next usage

               ' It is not in use.

                  ' Delete it.
                  Console.WriteLine("Deleting content type {0}...", obsolete.Name)
               End If

            ' No content type found.
               Console.WriteLine("The content type does not exist in this site collection.")
            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
   End Sub

End Module