I've never run into this problem before, but the variable I set in my module won't delete during uninstall. What am I doing wrong?

I set the variable in my module like this:

variable_set('marigold_nutrients', $nutrients);

All is good. The variable saves just fine and I'm able to use it as normal with variable_get.

Here is how I try to delete it:

In my module's .install file, I implement hook_uninstall...

 * Implements hook_uninstall().
function marigold_nutrition_uninstall() {

Using the UI, I remove the module then uninstall it. The database table the module creates deletes just fine, and it looks as though it's uninstalled. However, when I look at the variable table in Sequel Pro, the 'marigold_nutrients' variable is still there. I've run cron, cleared the cache... not sure what to do.

MORE INFO: - Using the debugger, I see that when I uninstall the module, it steps into the uninstall function just fine, so I'm pretty sure that function is working.

  • 1
    variable_del is the way to go. All Drupal modules use this function. This function deletes the relevant record AND clears the cache. Make sure that you look at the correct database and your spelling is correct, that the .install file is correct, double-check everything. – Wtower Dec 10 '14 at 15:36
  • I know, variable_del has always worked for me in the past. I've checked and double checked spelling... was up until after midnight last night trying to figure it out. I'm not sure what's wrong this time. – mlanning Dec 10 '14 at 15:39
  • 1
    Can you see the hook_uninstall() is called at all? the variable_get/set/del are small and straight forward functions and never had any problem using them. – AyeshK Dec 10 '14 at 15:46
  • Does it work with a db_query instead? (Even if it is dirty) – Djouuuuh Dec 10 '14 at 15:47
  • 1
    Is there something in the reports? – Djouuuuh Dec 10 '14 at 15:55

The problem is probably here (from the comments):

The variable gets set during installation when the hook_schema calls a function to iterate through an array to create database columns.

The uninstall process calls drupal_uninstall_schema(), which in turn calls drupal_get_schema_unprocessed(), which invokes hook_schema().

So the variable is being added again during uninstall, presumably at a later point in the process than your hook_uninstall() is invoked and variable_del() has run.

| improve this answer | |
  • Yes, that sounds like the answer. Do you know if there is a way to force the variable_del to run at the end of the uninstall process? – mlanning Dec 10 '14 at 20:46
  • There wouldn't be one in a normal workflow, you arguably shouldn't be setting a variable inside hook_schema() at all because its job is to provide data, not manipulate it. The proper place to set the variable in the first instance would be hook_install(). That should run before hook_schema() is invoked, so you can easily pick it up from there – Clive Dec 11 '14 at 11:06
  • I definitely want to set this up correctly and use best practices, as I'm very much new to this and still learning loads every day. I've tried setting the variable in hook_install, but it looks like hook_schema has already been run and built the tables before 'hook_install' is called. – mlanning Dec 13 '14 at 15:41

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.