SPContentTypeCollection.Delete method

Deletes the specified content type from the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Sub Delete ( _
    id As SPContentTypeId _
)
'Usage
Dim instance As SPContentTypeCollection
Dim id As SPContentTypeId

instance.Delete(id)
public void Delete(
    SPContentTypeId id
)

Parameters

Exceptions

Exception Condition
ArgumentOutOfRangeException

No content type in the collection has the specified content type ID.

SPException

The content type is the last content type on a list. The last content type on a list cannot be deleted.

SPException

The content type is the last content type on a list. The last content type on a list cannot be deleted.

The content type is the parent of a site or list content type. You cannot delete a content type that is in use.

-or-

The content type is part of an active Feature.

-or-

The content type collection is read-only.

SPContentTypeSealedException

The specified content type is sealed.

SPContentTypeReadOnlyException

The specified content type is read-only.

Remarks

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. SharePoint Foundation does not consider items sent to the Recycle Bin when making this determination. 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.

When specifying the ID of a content type to delete, keep in mind that the IDs of site and list content types are derived from the IDs of built-in content types but they are not the same as the built-in content type IDs. For example, the following line of code attempts to delete the Item content type by specifying SPBuiltInContentTypeId.Item as the content type ID:

list.ContentTypes.Delete(SPBuiltInContentTypeId.Item); // Throws an exception.

The code throws an ArgumentOutOfRangeException exception because the list’s content type collection does not include a content type with an ID of SPBuiltInContentTypeId.Item. It does, however, include a content type that has an ID that is derived from and is therefore a close match with the built-in content type ID. The following code illustrates the correct way to find and delete the list’s copy of the Item content type.

SPContentTypeId id = list.ContentTypes.BestMatch(SPBuiltInContentTypeId.Item);
list.ContentTypes.Delete(id);

Examples

The following example shows a console application that verifies whether an obsolete content type is in use in the current Web site or any child sites. If the content type is not in use, 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 the obsolete content type.
            Dim obsolete As SPContentType = webSite.ContentTypes("Test")

            If obsolete IsNot Nothing Then ' We have a content type
               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
                     Console.WriteLine(usage.Url)
                  Next usage

               Else ' It is not in use.
                  ' Delete it.
                  Console.WriteLine("Deleting content type {0}...", obsolete.Name)
                  webSite.ContentTypes.Delete(obsolete.Id)
               End If

            Else ' 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...")
      Console.ReadLine()
   End Sub

End Module
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"];

               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();
      }
   }
}

See also

Reference

SPContentTypeCollection class

SPContentTypeCollection members

Microsoft.SharePoint namespace

SPContentTypeUsage

Other resources

Content Type IDs

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy