Howto: Uninstall a field-based module.

Roman Zimmermann's picture
Roman Zimmermann

Usually when you try to uninstall a field-based module you're confronted with the following error message:

[module] is a required module and can't be disabled. Reason: Field type(s) in use - see Field list 

In this blogpost I'm showing you how to uninstall such a module anyway (deleting all the stored data).

0. Preparation

  • This howto makes heavy use of drush. You have it installed already, right?
  • NOTE: If you follow this howto you will permanently delete all data stored in those fields!

1. Delete all the field instances and fields.

This simple PHP-snippet should do the trick. Replace [module] with the name of the module that you need to uninstall and execute it via drush php-script

foreach (field_read_fields(array('module' => '[module]')) as $field) {
  foreach(field_read_instances(array('field_name' => $field['field_name'])) as $instance) {
    field_delete_instance($instance);
  }
}

If you have data stored in the field-tables this won't delete the data. It just renames the tables to field_deleted_data_X and field_deleted_revision_X. The actual deletion will happen in (possibly lots of runs of) field_cron(). If you want to disable the module now you probably will get the error:

[module] is a required module and can't be disabled. Reason: Fields pending deletion

2. Deleting field data

The cron-job is set up to delete the field data in small batches. This is because calling all those entity-hooks can take it's time. While it's possible to increase the batch size. I'd advise you not do to that just for uninstalling one module.

The simplest way to delete the data is to wait until cron was run often enough. If you want it to go faster you can use the following snippet to execute the batch deletion.

require_once drupal_get_path('module', 'field') . '/field.crud.inc';
field_purge_batch(10000);

or as a drush ev-oneliner:

drush ev "require_once drupal_get_path('module', 'field') . '/field.crud.inc';field_purge_batch(10000);"

Perhaps you still need to call it a few times.

3. Disable & uninstall the module as usual

drush dis -y [module]
drush pm-uninstall -y [module]

Share this!