The Drupal module uninstall snippet we should all use.

The Drupal module uninstall snippet we should all use.

Before I download a contributed module from Drupal.org I like to read the .install file (if one exists), this way I'm sure that the uninstall and install process are set to do what they are supposed.

Too many times I've seen a variable that was set (variable_set), not get deleted (variable_del). Then most of the variable sits in Drupal for the rest of the site's life.

I once saw this uninstall code in a module and have used it in all of my custom modules. Note: a coding best practice for me is to preset any variables I set with the "mymodulename_", then this below code will delete any variables I've set.


function hook_uninstall() {
  // Remove module variables.
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'hook_%'");
  while ($var_name = db_result($result)) {
    variable_del($var_name);
  }
}

P.S. Replace "hook" with your module name.

Comments

maroqqo's picture

Steve, good point! Agree! But

Steve, good point! Agree! But db_result() is an unknown function in D7. In D7 you should use

// may be rewritten in Drupal7 as
$val = db_query({...})->fetchField();

// another option would maybe
db_delete('variable')->condition('name', 'YOURMODULE_%', 'LIKE')->execute();

Something else to help you with a lil' bug report: registering with your (good looking :D ) page and twitter pointed out an error while the final step and signing in:

warning: copy() [function.copy]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/compwiza/public_html/drupal.prod/sites/stevesme.name/modules/rpx/rpx.module on line 876.
warning: copy(http://a0.twimg.com/profile_images/1419056422/maroqqo-logo-v3_2_normal.png) [function.copy]: failed to open stream: no suitable wrapper could be found in /home/compwiza/public_html/drupal.prod/sites/stevesme.name/modules/rpx/rpx.module on line 876.

hope it helps

sincerely,
Digidog

Steve Douglas's picture

Mucho gracias Digidog for all

Mucho gracias Digidog for all the comments on my blog. I had completely forgot to mention that the code snippet I provided in the post was from a D6 module, glad you not only picked up on it but gave insight how to do it D7 for any other possible readers.

I also looked into the error message you received when registering with the site and got it resolved with my host.