Best way to drop a field from a content type in Drupal

Features is a great module, one of the main feature of it is that allow us to create a content type in a local environment using the Drupal GUI and when is done export the content type as PHP code to be submitted to the SCM to later be imported in others environments.

This sounds really cool but to be honest this module will not solve all your needs related to content types, sadly something so stupid as field deletion can not be managed by this module and will need some extra work from you.

If you want to remove a field from a content type you can delete the field using the GUI and after that update your feature to remove the field from the feature as well, the thing is that we are just avoiding the tracking of the field in the feature, the feature will not longer take care of this field, the feature doesn’t know anything about this field now, so the feature will never know that must delete the field in others environments since is not his problem anymore.

As result the field will be deleted in our local environment (because you did that manually) but will be still present in others environments.

The proper way to solve this is using a hook_update to force the removal of the field in the others environments. To delete a field we have some methods in the Drupal API, these methods are

  •  field_info_field
  •  field_delete_field
  •  field_delete_instance
  •  field_purge_field

Field info Field will tell us information about the field, if the field is still in the database will bring some information, if not will return FALSE.

Field Delete field will remove the field from the database, all instances. So be aware that if you are sharing the field in several content types, the fill will be removed from all content types.

If you want to delete a shared field only from a single content type and keep the instance in others content types you must use Field delete instance.

At last, field purge field is used to complete remove all existence of the field from our system. Use this if your are totally sure that the field is not longer relevant and you are not going to use it anymore in any place.

Here is a sample about how to implement a hook_update.

function my_module_update_7029() {
  if (field_info_field('field_taxes')) {
    field_delete_field('field_taxes');
  }
}

If case that you don’t know, The field name can be read in the field management gui of the content type 😉

Conclusion

Keep in mind that you must handle manually the deletion of a field. It is a hard work but somebody must do it, as a good thing, Drupal have a good API for this so don’t panic.

The logical steps to delete a field will be.

  • Remove the field from your feature.
  • Check the state of the feature and verify you content type, your field will in the content type but not in the feature.
  • Create the hook update to remove the field.
  • Run the hook update locally.
  • Check that the field is not longer present in the content type.
  • Push your code !
  • Deploy your code and check in other environment that the field was removed correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.