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.
Posted by vacilando on November 9, 2010 at 11:33pm

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?

Categories: , , ,

Comments

Modules are supposed to

Posted by dalin on November 9, 2010 at 11:57pm

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.

Posted by vacilando on November 10, 2010 at 12:43am

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

Posted by dalin on November 10, 2010 at 2:46am

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

*_*

Posted by beautifulmind on November 10, 2010 at 5:17am

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

Posted by dalin on November 10, 2010 at 5:23am

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?

Posted by justintime on November 10, 2010 at 2:34pm

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

Posted by dalin on November 11, 2010 at 12:20am

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

Posted by dalin on November 11, 2010 at 3:32am

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

Posted by vacilando on November 10, 2010 at 11:36am

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

Posted by Garrett Albright on November 10, 2010 at 5:02am

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.

*_*

Posted by beautifulmind on November 10, 2010 at 5:19am

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

Posted by Garrett Albright on November 10, 2010 at 5:36am

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

Posted by justintime on November 10, 2010 at 2:43pm

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:

AltStyle によって変換されたページ (->オリジナル) /