Drupal 8 : How to use DELETE query with Syntax and Example

Using the object of core Drupal class we can perform database queries. Delete is the data deletion query used to remove data from Database based on conditions. The delete() is a function of Drupal 8 core class used for remove records. Below is syntax and example of how to use DELETE query in Drupal 8.

Syntax: 

// Delete the record from our table.
\Drupal::database()->delete('TABLE_NAME')
	->condition(CONDITION) // Condition for Remove specific records.
	->execute();
 
<u><strong>Example:</strong></u>
$query = \Drupal::database()->delete('employee', 'emp');
	->condition('employee_id', 'CE 003')
	->execute();

 

Before Delete Table be like:

EMPLOYEE_ID EMPLOYEE_NAME EMPLOYEE_AGE
CE 001 Mohan 20
CE 002 Dinesh 17
CE 003 Kumar 25

After Performing delete Query table records like:

EMPLOYEE_ID EMPLOYEE_NAME EMPLOYEE_AGE
CE 001 Mohan 20
CE 002 Dinesh 17
Category: