variable_del

5 bootstrap.inc variable_del($name)
6 bootstrap.inc variable_del($name)
7 bootstrap.inc variable_del($name)
8 bootstrap.inc variable_del($name)

Unsets a persistent variable.

Case-sensitivity of the variable_* functions depends on the database collation used. To avoid problems, always use lower case for persistent variable names.

Parameters

$name: The name of the variable to undefine.

See also

variable_get(), variable_set()

34 calls to variable_del()

File

includes/bootstrap.inc, line 634
Functions that need to be loaded on every Drupal request.

Code

function variable_del($name) {
  global $conf;

  db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
  cache_clear_all('variables', 'cache');

  unset($conf[$name]);
}

Comments

delete multiple variables with one call

It would be nice to be able to call this like unset(), passing n parameters and having them all deleted instead of making successive calls to the function for each variable.

Or even something like

<?php
function variable_contains_del($name) {
  global
$conf;

 
db_query("DELETE FROM {variable} WHERE name like '%%%s%%'", $name);
 
cache_clear_all('variables', 'cache');

  unset(
$conf[$name]);
}
?>

and the same thing for variable_beginswith_del (like '%s%%') and variable_endswith_del (like '%%%s')

Not sure about the escaped percents within single quotes, but this conveys the idea.

I saw this in the AddThis

I saw this in the AddThis module. Be sure to use the hook_uninstall function and replace addthis_% with your own module variable setting.

<?php
// Remove module variables.
 
$result = db_query("SELECT name FROM {variable} WHERE name LIKE 'addthis_%'");
  while (
$var_name = db_result($result)) {
   
variable_del($var_name);
  }
?>

Hitting the database to find

Hitting the database to find all your variables isn't necessary. You can use the global $conf variable.

<?php
global $conf;
foreach (
array_keys($conf) as $key) {
 
// It's important to use === instead of == with strpos()
 
if (strpos($key, 'addthis_') === 0) {
   
variable_del($key);
  }
}
?>

This is an approach I've used in various modules. It's lazy, but it hasn't failed me yet.

If you're removing all the

If you're removing all the variables set by your module in the uninstall function, it's much more efficient to do the following, since it saves multiple function calls and only clears the cache at the end.

The second db_query is the same one cache_clear_all('variables', 'cache'); makes, but without the overhead of a loop or additional wrapper functions.

<?php

function addthis_uninstall() {
 
// ... other code ...

 
db_query("DELETE FROM {variable} WHERE name LIKE 'addthis\_%'");
 
db_query("DELETE FROM {cache} WHERE cid='variables'");
}

?>

Just remember not to use this

Just remember not to use this approach if you do have several modules with names starting with the same string, for example mymodule, mymodule_submodule and mymodule_other_submodule.

However if you do have strict dependency (mymodule_submodule requires mymodule_other_submodule) , then it does not matter.

Login or register to post comments