In a custom module, how do I iterate through the content types, removing a specific field, and clearing the DB table+revision? I have refactored a field into a content type, and can convert data from the field to the new content, but wanted to have it scripted to remove the old way of doing things.

Comments

Pasting here, since the stackexchange answer includes the use of drush, which may or may not work in Backdrop.

The function that takes care of deleting the field in Backdrop is field_delete_field($field_name). This function will not only remove the db tables for the field values, but it will also remove all the field.instance... and field.... configuration json files from the config directory.

Notice also that sometimes the field's database table and config files may not be deleted right away, but be marked for future removal during cron (but the field will be inaccessible and will effectively be removed from the UI). I believe this is true if the field contained values, but I'm not sure.

Also note that if you want to remove a field programmatically from a specific bundle (e.g. from the Page content type) you can use field_delete_instance($instance_array);. You have to load the instance array first, e.g. for a node type called 'page', using $instance_array = field_info_instance('node', $my_field_name, 'page');

If you use TRUE as the second parameter of field_delete_instance(), then the field will be completely removed if it is not in use by other entities or bundles.