8.5.x database.inc db_delete($table, array $options = [])
8.0.x database.inc db_delete($table, array $options = array())
8.1.x database.inc db_delete($table, array $options = array())
8.2.x database.inc db_delete($table, array $options = array())
8.3.x database.inc db_delete($table, array $options = [])
8.4.x database.inc db_delete($table, array $options = [])
8.6.x database.inc db_delete($table, array $options = [])
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

2 calls to db_delete()
path_delete in includes/path.inc
Delete a URL alias.
_update_authorize_clear_update_status in modules/update/update.authorize.inc
Clears cached available update status data.

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' '!='),

kingandy’s picture

This is true except for when the supplied value is an array, when the default operator becomes 'IN'.

Moahammad Raheem’s picture

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

kingandy’s picture

Not in a single call. You just need to call db_delete twice though. See this comment for an example of a wrapper function that deletes the same record from two different tables.

You'd do better asking these sort of questions via the forums, people are more likely to see them and you'll get notified when somebody replies.

nicobot’s picture

I was looking for an example on how to use db_delete() with a Not Exists operator that can have a reference to the main table, and finally I've got to the solution:

$subquery = db_select('node', 'n')
    ->where('nid = feeds_item.entity_id');
  
$subquery->addField('n', 'nid');

$records = db_delete('feeds_item')
    ->condition('entity_type', 'node')
    ->notExists($subquery)
    ->execute();

I hope it helps somebody else.

ilclaudio’s picture

How to use db_delete with an array?
I mean a condition like "WHERE id IN (1,2,3,4)" where $myArray= (1,2,3,4).
Thank you

m-webtechnik’s picture

at db_select you can find the example
->condition('uid', array(1,5,7),'IN')

kingandy’s picture

The database API is clever enough to recognise an array and assume you want to use the 'IN' operator, so you just have to do
->condition('id', $myArray)