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?


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

function foo_uninstall() {

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.

| improve this answer | |

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.

| improve this answer | |
  • This will delete variables for other modules like "examplemodule_extreme", too. – user7 Apr 18 '11 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 '11 at 18:31
  • 1
    @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 '11 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 '11 at 19:09
  • 2
    Well, some core modules might not have any variables and some are required and can't be uninstalled like node and user. So it's pointless for these to implement hook_uninstall(). That said, it's perfectly possible that some variables aren't correctly uninstalled even in core. It's something that is easily forgotten. I invested many hours myself trying to get hook_variable_info() into core but that is simply not going to happen with the current variable system, it makes it too complex and too slow. – Berdir Apr 18 '11 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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