Any method for clearing unused values from the 'variable' table?

Events happening in the community are now at Drupal community events on www.drupal.org.
vacilando's picture

After a Drupal install has been used a while, the table 'variable' overflows with values set by various disabled modules or obsolete functions which do need clearing because variable_init() (bootstrap.inc) does that unfortunate step of caching all of the table, which then often causes sites to crash due to lack of PHP memory.

Module developers may choose to use functions alternative to variable_set() and variable_get() provided by http://drupal.org/project/queryable_variables but that does not solve the pollution in all the existing 'variable' tables.

Basically what's needed is a function or module that would look up variable names in all of the enabled modules and delete those variables that are no longer in use.

Has anybody seen a solution for this, or perhaps dealt with this in another way?

Comments

Modules are supposed to

dalin's picture

Modules are supposed to cleanup variables in hook_uninstall().

Be sure to uninstall modules before deleting their files.

--


Dave Hansen-Lange
Director of Technical Strategy, Advomatic.com
Pronouns: he/him/his

That's right, dalin.

vacilando's picture

That's right, dalin. Unfortunately, many of them don't do it, or there are forgotten leftovers. I try to run uninstall before removing modules, but still, after several months or years, all of the sites I manage have polluted variable tables.

So whilst I agree that good discipline may prevent that table's pollution, I am still looking (and I believe I am not alone) for a script or module that would safely identify all unused variables and delete them from table 'variable'.


---
Tomáš J. Fülöpp
http://twitter.com/vacilandois

You've inspired me to create

dalin's picture

You've inspired me to create a module:
http://drupal.org/project/variable_clean

--


Dave Hansen-Lange
Director of Technical Strategy, Advomatic.com
Pronouns: he/him/his

*_*

beautifulmind's picture

The module concept is very nice. I believe, the module will present an interface listing all the variables and the admin will delete those not needed any more.
Do we have a method or way with which we can bind a variable with a module name? like in system table? (this is a general query)

I'd love to use the variable clean module.

Thanks.

The module lists unused

dalin's picture

The module lists unused variables and allows you to delete them. You can download it from CVS to try it out, or wait until the d.o module build script runs (every 12hrs for dev versions of modules).

--


Dave Hansen-Lange
Director of Technical Strategy, Advomatic.com
Pronouns: he/him/his

Dynamic names?

justintime's picture

What does your module do about modules that use dynamic variables? IE:

<?php
variable_set
('mymodule_'. $node->type .'_something', 1);
?>

A lot of them out there do this sort of thing. Unfortunately, there's not a forced namespace either.

It uses the first static

dalin's picture

It uses the first static portion to create a wildcard. so it this is found in code:

<?php
variable_set
('mymodule_'. $node->type .'_something', 1);
?>

Then the following variables are considered 'used':

mymodule_page_something
mymodule_
mymodule_something_totally_unrelated

It also handles this case:

<?php
variable_set
("mymodule_$nodetype", 1);
?>

And it will bail if it finds something like this:

<?php
variable_set
($foo. "_mymodule", 1);
?>

--


Dave Hansen-Lange
Director of Technical Strategy, Advomatic.com
Pronouns: he/him/his

Now with simpletests for

dalin's picture

Now with simpletests for testing all manner of bizarre PHP syntax (which revealed a few bugs on really obscure syntax).
Also it now prints out helpful info when it finds code that prevents it from making an accurate determination.

--


Dave Hansen-Lange
Director of Technical Strategy, Advomatic.com
Pronouns: he/him/his

Wow! I you did not, I would

vacilando's picture

Wow! I you did not, I would have made it :-) Thanks!


---
Tomáš J. Fülöpp
http://twitter.com/vacilandois

An aside for coders… Yeah, so

Garrett Albright's picture

An aside for coders…

Yeah, so sometimes you hack away and you add a variable_set() to your code and you forget to put a variable_del() for it in your hook_uninstall(). That's why I always use code like this in my hook_uninstall()s…

<?php
/**
* Implementation of hook_uninstall().
*/
function mymodule_uninstall() {
  global
$conf;
  foreach (
array_keys($conf) as $key) {
    if (
strpos($key, 'mymodule_') === 0) {
     
variable_del($key);
    }
  }
}
?>

Now I can create my variables with impunity and not have to worry about deleting them!

But what if someone creates a mymodule_improvement module which uses variables that start with mymodule_improvement_ (and therefore mymodule_)? Well, if they're dependent on your module, their module will have to be uninstalled (and its variables therefore deleted) first anyway, so still, no worries.

Okay, back to your regularly scheduled thread.

*_*

beautifulmind's picture

This code works best when the variable is prefixed by mymodule_. but most of the time, when creating admin interface, most of the developer do not follow this practice.

Thanks for sharing the code. I really appreciate it.

Regards.

Yes, this module assumes the

Garrett Albright's picture

Yes, this module assumes the developer has followed standard Drupal practices in terms of choosing their variable names. But I guess you know what they say about assuming…

This is a no-no

justintime's picture

Garret, while that works, there's a very good chance your module might delete some other module's variables. The problem is that the "other" module may have no dependency on your module. Imagine the 'facebook' module - how many variables for other modules would the code above delete?

Aaron Winborn posted a nice solution to this on the development mailing list last month. Here's the start of that thread.

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: