You are here

function nodeapi_example_uninstall in Examples for Developers 7

Same name and namespace in other branches
  1. 6 nodeapi_example/nodeapi_example.install \nodeapi_example_uninstall()

Implements hook_uninstall().

We need to clean up our variables data when uninstalling our module.

Our implementation of nodeapi_example_form_alter() automatically creates a nodeapi_example_node_type_<contentType> variable for each node type the user wants to rate.

To delete our variables we call variable_del for our variables' namespace, 'nodeapi_example_node_type_'. Note that an average module would have known variables that it had created, and it could just delete those explicitly. For example, see render_example_uninstall(). It's important not to delete variables that might be owned by other modules, so normally we would just explicitly delete a set of known variables.

hook_uninstall() will only be called when uninstalling a module, not when disabling a module. This allows our data to stay in the database if the user only disables our module without uninstalling it.

Related topics

File

nodeapi_example/nodeapi_example.install, line 70
Install, update and uninstall functions for the nodeapi_example module.

Code

function nodeapi_example_uninstall() {

  // Simple DB query to get the names of our variables.
  $results = db_select('variable', 'v')
    ->fields('v', array(
    'name',
  ))
    ->condition('name', 'nodeapi_example_node_type_%', 'LIKE')
    ->execute();

  // Loop through and delete each of our variables.
  foreach ($results as $result) {
    variable_del($result->name);
  }
}