Delete queries
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Delete queries must always use a query builder object. They are started using the db_delete() function as follows:
$query = db_delete('node', $options);
That creates a delete query object that will delete records from the node table. Note that braces are not required around the table name as the query builder will handle that automatically.
The delete query object uses a fluent API. That is, all methods (except execute()) return the query object itself allowing method calls to be chained. In many cases, this means the query object will not need to be saved to a variable at all.
Delete queries are conceptually very simple, consisting of only a WHERE clause. The full structure of the WHERE clause is detailed in the section on Conditional clauses, and will only be touched on here.
A full Delete query will take the following form:
$num_deleted = db_delete('node')
->condition('nid', 5)
->execute();
The above query will delete all rows from the {node} table where the nid column is 5. It is equivalent to the following query:
DELETE FROM {node} WHERE nid=5;
The execute() method will return the number of records that were deleted as a result of the query.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.