Remove any information that the module sets.

Remove any information that the module sets.

The information that the module should remove includes:

  • variables that the module has set using variable_set() or system_settings_form()
  • modifications to existing tables

The module should not remove its entry from the {system} table. Database tables defined by hook_schema() will be removed automatically.

The uninstall hook must be implemented in the module's .install file. It will fire when the module gets uninstalled but before the module's database tables are removed, allowing your module to query its own tables during this routine.

When hook_uninstall() is called, your module will already be disabled, so its .module file will not be automatically included. If you need to call API functions from your .module file in this hook, use drupal_load() to make them available. (Keep this usage to a minimum, though, especially when calling API functions that invoke hooks, or API functions from modules listed as dependencies, since these may not be available or work as expected when the module is disabled.)


pillarsdotnet’s picture

The code inside a hook_uninstall() implementation should not call any API functions that are provided by any .module file, including its own, unless:

  1. The uninstall code explicitly loads the module file, e.g., by calling drupal_load().
  2. It is absolutely certain that the called API function does not rely on other API functions which may not be available.

See also:

Karlheinz’s picture

Due to a bug in Drupal core, comments aren't unistalled properly. You'll likely get the error in the subject line.

Here is a temporary fix, until core is patched:

  $instance = array(
    'field_name' => 'comment_body',
    'bundle' => 'comment_node_' . $my_node_type,
    'entity_type' => 'comment',

I got the code from here:

Karlheinz’s picture

Apologies; the above code does not work - you still get the error.

For the record, I also tried adding field_delete_field('comment_node_' . $my_node_type) afterwards; still no luck.

charubachi’s picture

Can we use hook_uninstall for schema with foreign_key?

kingandy’s picture

The foreign_key property in the schema only exists for informational purposes so probably doesn't affect anything in hook_uninstall().

Note that there is no need to schema_uninstall() anything in your hook_uninstall() - this will be done automatically. You do have to remove any manual alterations you made to the database in hook_install() though (eg extending or altering some other module's table).