How to delete field programmitically using hook_update? We have a situation where in different environments we have to manually delete fields. Rather than manually doing that, we want to programmatically deploy the changes. The idea that is being is discussed is to use a custom module which calls the hook_field_delete_instance() hook. 

The issue we are having is how to best pass in the content type machine name and the machine name for a single content type. We are understanding that when we reused the field on a separate content type, we are essentially creating an alias on the current content type that points to the original content type holding the field name. 

Using the situation below, what is the best way to delete the two fields on the on the basic page only using the hook_field_delete_instance() on a custom module? 

Situation:

We have two content types: article and basic page. We created several fields on the article content type that we ended up reusing on the basic page. 

Article:


field_art_original_publish_date
field_art_country_tag
field_art_image


Basic Page:


field_art_image (reused from article content type)
field_art_country_tag (reused from article content type)




Update:

So now that i am thinking further on this, i would like 1 hook update for 1 field deletion so that if a hook update fails then i know which fields need attention. 

Here is what i have come up with using what @Clive has suggested. 

function mymodule_update_7001(&amp;$sandox) {
     mymodule_delete_field('field_art_image', 'page' );
}

function mymodule_update_7002(&amp;$sandox) {
    mymodule_delete_field('field_art_country_tag', 'page');
}

function mymodule_delete_field($field_mn, $ct_mn) {
    $entity_type = "node";

    $field_instance = field_info_instances($entity_type, $field_mn, $ct_mn);
    field_delete_instance($field_instance);

    //setting the batch size to a large number resolves my prior issues
    $batch_size = 20000;
    field_purge_batch($batch_size);
}
We are understanding that when we reused the field on a separate content type, we are essentially creating an alias on the current content type that points to the original content type holding the field name.


Not quite - each instance exists in its own right, unaffected by any other instance of the same field on another entity type/bundle. Removing it from one bundle (content type) won't touch it on another bundle, regardless of which one it was originally attached to.

You won't need hook_field_delete_instance() for this, just a combination of field_info_instance() and field_delete_instance() in an update hook. Something like this:

/**
 * Remove old fields from the Basic Page content type.
 */
function MYMODULE_update_7100() {
  $field_art_image = field_info_instance('node', 'field_art_image', 'page');
  field_delete_instance($field_art_image);

  $field_art_country_tag = field_info_instance('node', 'field_art_country_tag', 'page');
  field_delete_instance($field_art_country_tag);

  return 'Fields deleted, data will be cleared up on cron.';
}