1 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

File

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

Code

function field_delete_instance($instance, $field_cleanup = TRUE) {
  // Sanity check to prevent writing a new empty config file.
  field_validate_instance($instance, TRUE, FALSE);

  // Mark the field instance for deletion.
  $config = config('field.instance.' . $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name']);
  $config->set('deleted', 1);
  $config->save();

  // 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);
  token_cache_clear();

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