Delete queries

Lastly, we can also get rid of our records using the DELETE query:

$result = $database->delete('players')  ->condition('name', 'Micheal P.')  ->execute();

All the records that match the condition will get removed. Be careful with this because as with update queries, leaving out a condition will basically truncate your table, and the query will return the number of records affected, that is, deleted.

Although you can make SELECT queries against entity and field tables to find the IDs of the entities you want to load, you should never perform INSERT, UPDATE, or DELETE queries against these tables. You run a very high risk of corrupting your data.

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.