7.x field.crud.inc field_delete_instance($instance, $field_cleanup = TRUE)

Marks a field instance and its data for deletion.

Parameters

$instance: An instance structure.

$field_cleanup: If TRUE, the field will be deleted as well if its last instance is being deleted. If FALSE, it is the caller's responsibility to handle the case of fields left without instances. Defaults to TRUE.

Related topics

8 calls to field_delete_instance()
CommentFieldsTest::testCommentDefaultFields in modules/comment/comment.test
Tests that the default 'comment_body' field is correctly added.
FieldBulkDeleteTestCase::testDeleteFieldInstance in modules/field/tests/field.test
Verify that deleting an instance leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and instance.
FieldBulkDeleteTestCase::testPurgeField in modules/field/tests/field.test
Verify that fields are preserved and purged correctly as multiple instances are deleted and purged.
FieldBulkDeleteTestCase::testPurgeInstance in modules/field/tests/field.test
Verify that field data items and instances are purged when an instance is deleted.
FieldInstanceCrudTestCase::testDeleteFieldInstance in modules/field/tests/field.test
Test the deletion of a field instance.

... See full list

File

modules/field/field.crud.inc, line 768
Field CRUD API, handling field and field instance creation and deletion.

Code

function field_delete_instance($instance, $field_cleanup = TRUE) {
  // Mark the field instance for deletion.
  db_update('field_config_instance')
    ->fields(array('deleted' => 1))
    ->condition('field_name', $instance['field_name'])
    ->condition('entity_type', $instance['entity_type'])
    ->condition('bundle', $instance['bundle'])
    ->execute();

  // Clear the cache.
  field_cache_clear();

  // Mark instance data for deletion.
  $field = field_info_field($instance['field_name']);
  module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance);

  // Let modules react to the deletion of the instance.
  module_invoke_all('field_delete_instance', $instance);

  // Delete the field itself if we just deleted its last instance.
  if ($field_cleanup && count($field['bundles']) == 0) {
    field_delete_field($field['field_name']);
  }
}

Comments

msellers’s picture

Pay close attention to the default value of $field_cleanup as the default is destructive of parent fields of the instance. This seems to go against the grain of reusing fields across various entities!

DamienMcKenna’s picture

It only removes the parent field if there are no other instances of it, so this should be fine.

hefox’s picture


  if ($instance = field_info_instance('node', 'field_name', 'node_type')) {
    field_delete_instance($instance);
  }
acbramley’s picture

Thanks this is perfect!

ranelpadon’s picture

// List of all machine names of taxonomy vocabularies affected.
// This includes Region, Vertical, City, Country, and Tags terms.
$bundles = array(
  'vocabulary_10',
  'vocabulary_22',
  'vocabulary_27',
  'vocabulary_28',
  'vocabulary_29',
);

// Iterate over the affected content types.
foreach ($bundles as $bundle) {
  // Get the fields' info array for the given content type.
  $field_instance = field_info_instances('taxonomy_term', $bundle);
  
  // Get the the value/instance of the Ad Tag folder field.
  $instance = $field_instance['field_ad_tag_folder'];
  
  // Delete the ad tag folder field instance.
  // This will also delete the ad tag folder field automatically
  // if the last instance/object of this field had been deleted.
  field_delete_instance($instance);
}