8.2.x database.inc db_delete($table, array $options = array())
8.0.x database.inc db_delete($table, array $options = array())
8.1.x database.inc db_delete($table, array $options = array())
8.3.x database.inc db_delete($table, array $options = array())
7.x database.inc db_delete($table, array $options = array())

Returns a new DeleteQuery object for the active database.

Parameters

$table: The table from which to delete.

$options: An array of options to control how the query operates.

Return value

DeleteQuery A new DeleteQuery object for this connection.

Related topics

171 calls to db_delete()
ActionLoopTestCase::testActionLoop in modules/simpletest/tests/actions.test
Set up a loop with 3 - 12 recursions, and see if it aborts properly.
actions_delete in includes/actions.inc
Deletes a single action from the database.
aggregator_aggregator_remove in modules/aggregator/aggregator.processor.inc
Implements hook_aggregator_remove().
aggregator_categorize_items_submit in modules/aggregator/aggregator.pages.inc
Form submission handler for aggregator_categorize_items().
aggregator_expire in modules/aggregator/aggregator.processor.inc
Expires items from a feed depending on expiration settings.

... See full list

File

includes/database/database.inc, line 2536
Core systems for the database layer.

Code

function db_delete($table, array $options = array()) {
  if (empty($options['target']) || $options['target'] == 'slave') {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->delete($table, $options);
}

Comments

drclaw’s picture

Simple example taken from http://drupal.org/node/310081

<?php

// Drupal 7
$nid = 5;
$num_deleted = db_delete('node')
  ->condition('nid', $nid)
  ->execute();

// Above example is equivalent to the following in Drupal 6
$nid = 5;
db_query("DELETE FROM {node} WHERE nid = %d", $nid);

?>
Whiskey’s picture

Another simple example, but with two conditional clauses, based on the Conditional clauses documentation at http://drupal.org/node/310086

$and = db_and()->condition('mid', 1)->condition('cache_type', 'year');
db_delete('mid_cache_index')->condition($and)->execute();   

// Above translates to:
DELETE FROM {mid_cache_index}  WHERE ( (mid = 1) AND (cache_type = 'year') )
Vlad Stratulat’s picture

I believe It's better to write this way instead of adding db_and() in this case. Fix me if I'm wrong.

db_delete('mid_cache_index')
  ->condition('mid', 1)
  ->condition('cache_type', 'year')
  ->execute();
DamienMcKenna’s picture

For simple uses cases doing it like that is best, but splitting it out as separate variables and adding them is best when you need to group multiple statements together using db_or().

rishabh318’s picture

Hi All,

I have this query. don't know why this query gives an error.


$and = db_and()->condition('uid', $form_state['values']['uid'])->condition('pfid', $pfid->pfid)->condition('key', $key);
db_delete('uc_file_users')->condition($and)->execute();

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = '\'SVTgikKQVSoimianTHBXGZ5vDXXa8_JAI1MiR5kHtsg\'') )' at line 1: DELETE FROM {uc_file_users} WHERE ( (uid = :db_condition_placeholder_0) AND (pfid = :db_condition_placeholder_1) AND (key = :db_condition_placeholder_2) ); Array ( [:db_condition_placeholder_0] => 2 [:db_condition_placeholder_1] => 3 [:db_condition_placeholder_2] => 'SVTgikKQVSoimianTHBXGZ5vDXXa8_JAI1MiR5kHtsg' ) in project_licenses_remove_submit() (line 821 of ......

Please Help me.

DeeZone’s picture

You're doing something funky in your code where you're building $key.

'key = '\'SVTgikKQVSoimianTHBXGZ5vDXXa8_JAI1MiR5kHtsg\'') )'

has odd escape values with the quotations. I'm guessing you're looking for:

$key = 'SVTgikKQVSoimianTHBXGZ5vDXXa8_JAI1MiR5kHtsg' rather than '\' ... and ... \'') )'. A better place to ask this type of support question would be in Stack Exchange: http://drupal.stackexchange.com as this is a Drupal API page - more about documentation.

mikelangeloff’s picture

/**
 * Implements hook_menu_delete().
 */
function block_menu_delete($menu) {
  db_delete('block')
    ->condition('module', 'menu')
    ->condition('delta', $menu['menu_name'])
    ->execute();
  db_delete('block_role')
    ->condition('module', 'menu')
    ->condition('delta', $menu['menu_name'])
    ->execute();
}
HansKuiters’s picture

I was surprised not needed to use an operand in the condition.
... ->condition('column', 'value')....
and
... ->condition('column', 'value', '=')....
both seems to work fine.

drakythe’s picture

In all db_ functions I am aware of in d7 the = operator is assumed on conditions when one is not specified. The operand is required if you want something OTHER than =, for instance

... ->condition('column', 'value' '!='),

Moahammad Raheem’s picture

is there any way to delete multiple table using db_delete()