Created
October 23, 2013 08:44
-
-
Save ThomasHambach/7114893 to your computer and use it in GitHub Desktop.
Delete ghost or dead fields in your Drupal 7 website. Sometimes you will end up with dead fields in your database after altering a feature that contained content types. This gist will get all the linked fields from your entities, all the fields defined in the database, find out the difference between them, and remove them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get all fields linked to entities. | |
$active = []; | |
$entities = field_info_instances('node'); | |
foreach ($entities as $entity) { | |
foreach ($entity as $field_name => $field_info) { | |
$active[] = $field_name; | |
} | |
} | |
$active = array_unique($active); | |
// Get all fields in database. | |
$all = []; | |
$result = db_query('SELECT field_name FROM {field_config}'); | |
foreach ($result as $record) { | |
$all[] = $record->field_name; | |
} | |
$all = array_unique($all); | |
// Find out the dead fields, and delete them. | |
$dead = array_diff($all, $active); | |
foreach ($dead as $field) { | |
field_delete_field($field); | |
} | |
// Purge cache | |
field_purge_batch(count($dead)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment