0

I have many variables set through variable_set. Is there any other way to delete them all at once apart from variable_del?

or How can find all the persistent variables set in the module?

2

It is considered bad practice to delete all the persistent variables whose name starts with the short name of the module. There could be a module whose short name starts with that short name; in that case you would be removing the persistent variables of that module.
For example, given the Views module, there could be a Views Enhancer module. Deleting all the persistent variable matching the SQL pattern '%views_%' would mean deleting the persistent variables of both modules.

Instead, you should have a list of all the persistent variables your module uses. (That is not hard, since you are writing the code of your module.) Using that list (for example, contained in $variables), a code like the following would delete all the variables.

  global $conf;

  db_delete('variable')
    ->condition('name', $variables, 'IN')
    ->execute();
  cache_clear_all('variables', 'cache_bootstrap');

  foreach ($variables as $name) {
    unset($conf[$name]); 
  }

This is, essentially, the code of variable_del() adapted to delete a list of variable names contained in $variables.

You could also use that code to implement a function your module uses.

function variable_delete_multiple(array $variables) {
  global $conf;

  db_delete('variable')
    ->condition('name', $variables, 'IN')
    ->execute();
  cache_clear_all('variables', 'cache_bootstrap');

  foreach ($variables as $name) {
    unset($conf[$name]); 
  }
}
0

You can always use MySQL to delete persistent variables set by variable_set:

delete from variable where name like '%module_name_variable_name%';

but assuming that you want to delete those variables on every page load consider another way to implement your code than using variable_get which is using the caching system

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.