7

I'm working on a Drupal 6 module that adds some variables to the variable table. How can I go about having those variables removed from the variable table when the module is uninstalled?

7
  • UPDATE: On further digging through the discussion on this blog post, I ran across this idea from Dave Reid to adopt the method that Drupal core uses.
    – Matt V.
    Apr 18, 2011 at 18:12
  • This will delete variables for other modules like "examplemodule_extreme", too.
    – user7
    Apr 18, 2011 at 18:18
  • @Mark Trapp: Yeah, there's some discussion of that in the comments on the post. I've updated my answer with a link to to a better idea in that discussion.
    – Matt V.
    Apr 18, 2011 at 18:31
  • @Matt I'd still be leery about using a new, generic hook like that for fear of namespace collision, but that's definitely a better alternative. And to be clear, the method Dave Reid uses (implementing hook_variables()) is not how core does it: that's how he does it in some of his own modules. In Drupal 8, this problem might be solved with the addition of a core hook_variable_info() hook.
    – user7
    Apr 18, 2011 at 18:39
  • @Mark Trapp: Yeah, I made the assumption that core did it that way based on the subject of Mark's reply. I did some digging though and was surprised to find that quite a few core modules still don't yet implement hook_uninstall at all.
    – Matt V.
    Apr 18, 2011 at 19:09

1 Answer 1

13

Keep track of your variables and add appropriate variable_del() calls within an implementation of hook_uninstall():

function foo_uninstall() {
  variable_del('foo_variable1');
  variable_del('foo_variable2');
}

As hook_uninstall() is an installation hook, it needs to reside in your module's .install file (e.g. foo.install).

You ought not to delete all variables prefixed with your module's name as namespace collisions will come back to bite you. For example, if your module's name is foo, you might think you should just poll the variables table for all variables prefixed foo_. But what if I write a module named foo_awesome? You'd delete my variables, too. That wouldn't be nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.