Variable install and uninstall script

Public

Snippet to correctly set variables during module installation process and correctly clean the mess after uninstalling the module.

</> CopyGet raw version
php
  1. /**
  2. * Implementation of hook_install().
  3. */
  4. function HOOK_install() {
  5. variable_set('MODULE_variable', 0);
  6. }
  7.  
  8. /**
  9. * Implementation of hook_uninstall().
  10. */
  11. function HOOK_uninstall() {
  12. // Get global variable array
  13. global $conf;
  14. foreach (array_keys($conf) as $key) {
  15. // Find variables that have the module prefix
  16. if (strpos($key, 'MODULE_') === 0) {
  17. variable_del($key);
  18. }
  19. }
  20. }

Comments

pierrelbz's picture

for drupal 7 you can do this :

  1. function MODULE_uninstall() {
  2. db_query("DELETE FROM {variable} WHERE name LIKE 'MODULE_%'");
  3. }