Working with Database in Drupal 7 - Lesson 10 - Delete Requests (DELETE)
Delete queries should use the query builder. They begin with the db_delete()
function:
This delete query will remove records from the node
table. Note that you do not need to wrap the table name in curly braces—Drupal's query builder handles that automatically. Delete queries use a Fluent API, meaning all methods (except execute()
) return the query object itself, just like update and insert queries.
Delete queries are simple and only use WHERE
expressions. We’ll cover WHERE
conditions in more detail in a future lesson, but for now let’s look at a basic delete query:
condition('nid', 5) ->execute(); ?>
This query will delete all rows from the node
table where nid = 5
.
This is equivalent to the following SQL statement:
DELETE FROM {node} WHERE nid=5;
The execute()
method returns the number of rows that were deleted as a result of the query.