Programmatically delete a field in Drupal 7

Last updated on 30/10/2016

There are two ways to delete a field in Drupal 7

1. Delete just the instance of a field for specific type of entity.

By default, the field itself is going to be deleted if the last instance is being deleted. To avoid this, pass FALSE value as second parameter.

// Delete just the instance of a field for specific type of entity
if ($instance = field_info_instance('node', 'field_related_reviews', 'article')) {
  field_delete_instance($instance);
}

2. In order to delete all the riff-raff from everywhere use field_delete_field()

field_delete_field('field_related_reviews');

Remember that this will just mark the field/records to be deleted on Cron run.

Use field_purge_batch() to actually delete the field, and all the recorrds in DataBase e.g:

field_delete_field('field_related_reviews');
$enough_range_to_delete_everything = 9999999;
field_purge_batch($enough_range_to_delete_everything);