Community Documentation

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 functions call variable_del()

aggregator_uninstall in modules/aggregator/aggregator.install
Implementation of hook_uninstall().
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
comment_node_type in modules/comment/comment.module
Implementation of hook_node_type().
comment_update_6002 in modules/comment/comment.install
Changed comment settings from global to per-node -- copy global settings to all node types.
contact_uninstall in modules/contact/contact.install
Implementation of hook_uninstall().
drupal_cron_cleanup in includes/common.inc
Shutdown function for cron cleanup.
drupal_cron_run in includes/common.inc
Executes a cron run when called
example_profile_tasks in developer/example.profile
Perform any final installation tasks for this profile.
forum_taxonomy in modules/forum/forum.module
Implementation of hook_taxonomy().
forum_uninstall in modules/forum/forum.install
Implementation of hook_uninstall().
hook_node_type in developer/hooks/node.php
Act on node type changes.
hook_uninstall in developer/hooks/install.php
Remove any information that the module sets.
install_tasks in ./install.php
Tasks performed after the database is initialized.
locale_node_type in modules/locale/locale.module
Implementation of hook_node_type().
locale_uninstall in modules/locale/locale.install
Implementation of hook_uninstall().
locale_update_6005 in modules/locale/locale.install
Change language setting variable of content types.
menu_rebuild in includes/menu.inc
(Re)populate the database tables used by various menu functions.
node_access_needs_rebuild in modules/node/node.module
Flag / unflag the node access grants for rebuilding, or read the current value of the flag.
node_type_form_submit in modules/node/content_types.inc
Implementation of hook_form_submit().
profile_uninstall in modules/profile/profile.install
Implementation of hook_uninstall().
search_uninstall in modules/search/search.install
Implementation of hook_uninstall().
statistics_uninstall in modules/statistics/statistics.install
Implementation of hook_uninstall().
syslog_uninstall in modules/syslog/syslog.install
Implements hook_uninstall().
system_settings_form_submit in modules/system/system.module
Execute the system_settings_form.
system_themes_form_submit in modules/system/system.admin.inc
Process system_themes_form form submissions.
system_theme_settings_submit in modules/system/system.admin.inc
Process system_theme_settings form submissions.
system_update_6017 in modules/system/system.install
Rename settings related to user.module email notifications.
system_update_6021 in modules/system/system.install
Migrate the menu items from the old menu system to the new menu_links table.
system_update_6036 in modules/system/system.install
Change the search schema and indexing.
system_update_6042 in modules/system/system.install
Upgrade recolored theme stylesheets to new array structure.
system_update_6044 in modules/system/system.install
RC1 to RC2 index cleanup.
update_settings_submit in modules/update/update.settings.inc
Submit handler for the settings tab.
update_uninstall in modules/update/update.install
Implementation of hook_uninstall().
_update_remove_update_status_variables in modules/update/update.install
Private helper to clear out stale variables from update_status 5.x contrib.

File

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

Code

<?php
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'");
}

?>

Login or register to post comments