I've used every possible combination of field_delete_field and field_delete_instance in my modules' uninstall function but none of these ever seem to cleanup all of the fields and instances I have created during the install. I also have a field_purge_batch(1000) call after doing the deletes.

Can anyone give me a good working example?

Comments

bass28’s picture

Found what seems to work...

First loop through the instances calling field_delete_instance($instance, true);
Then loop through the fields using field_delete_field($field['field_name']);
Then field_purge_batch(1000);

fuerst’s picture

You may alternatively just call field_purge_batch(n); where n is greater the count of all entities (aka: data or nodes) using the field.

Background: field_purge_batch() itself does delete data by using field_purge_data() but is limited to n entities only. When having at least 1 entity left the field will not be deleted.

Also you need to run it twice: First run is for deleting all data, second run is for deleting the field(s) itself. You may wrap it up into a hook_update_N() and use drush updatedb instead of the Drupal Web UI (/update.php).