1.20.x field.crud.inc | field_delete_field($field_name) |
Marks a field and its instances and data for deletion.
Parameters
$field_name: The field name to delete.
Related topics
File
- modules/
field/ field.crud.inc, line 423 - Field CRUD API, handling field and field instance creation and deletion.
Code
function field_delete_field($field_name) {
// Delete all non-deleted instances.
$field = field_info_field($field_name);
// Sanity check to prevent writing a new empty config file.
field_validate_field($field, TRUE);
if (isset($field['bundles'])) {
$instances = field_info_instances();
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$instance = $instances[$entity_type][$bundle][$field_name];
field_delete_instance($instance, FALSE);
}
}
}
// Mark field data for deletion.
module_invoke($field['storage']['module'], 'field_storage_delete_field', $field);
// Mark the field for deletion.
$config = config('field.field.' . $field_name);
$config->set('deleted', 1);
$config->save();
// Clear the cache.
field_cache_clear();
module_invoke_all('field_delete_field', $field);
// Do a purge to delete any empty tables immediately.
field_purge_batch(0);
}