This documentation is archived and is not being maintained.

SPContentType.Delete Method

Windows SharePoint Services 3

Deletes the content type.

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

public void Delete()

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 then 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. Windows SharePoint Services does not consider items sent to the Recycle Bin when making this determination. If items of a specific content type are restored to a list after their content type has been deleted from that list, then those items are assigned the default content type for that list.

TipTip

You might want to consider an alternative to deleting items simply because their content type is obsolete. Try leaving the outmoded content type in place and setting its Hidden property to true. This removes the content type from the New menu on any list where the content type is used, thus preventing users from adding new items of that content type to the list while at the same time preserving the content of existing items.

The following example consists of two methods from a larger application. The first method, DeleteListContentType, accepts an SPContentType object as its only argument. The method first verifies that the object is derived from a list’s content type collection. Then the method calls a second method, DeleteListItems, passing the list and the content type ID as arguments. The second method searches the list for items of the specified content type and deletes them. When control is returned to the DeleteListContentType method, it deletes the content type.

static bool DeleteListContentType(SPContentType ct)
{
   // Make sure we have a content type.
   if (ct == null)
      throw new ArgumentException("Content type is null.");

   // Make sure we have a list content type.
   if (ct.ParentList == null)
      return false;

   // Delete list items of this content type.
   DeleteListItems(ct.ParentList, ct.Id);

   // Check for read-only and sealed.
   if (ct.ReadOnly)
      ct.ReadOnly = false;
   if (ct.Sealed)
      ct.Sealed = false;

   // Delete it.
   ct.Delete();
   return true;
}

static void DeleteListItems(SPList list, SPContentTypeId id)
{
   SPListItemCollection items = list.Items;
   int count = items.Count;  //Count will change
   for (int i = count -1; i >= 0; i--) 
   {
      SPListItem item = items[i];
      if (item.ContentType.Id == id)
         item.Delete();
   }
   list.Update();
}
Show: