The DELETE statement in MySQL is used to remove one or more records from a table. When working with PHP and MySQL, this command is commonly executed through database queries to permanently remove unwanted data.
WHERE, all records are deletedmysqli or PDO
// Basic DELETE syntax in MySQL
DELETE FROM table_name WHERE condition;
This query deletes the employee whose ID is 5 from the employees table.
// Delete a specific employee by ID
DELETE FROM employees WHERE employee_id = 5;
This query removes all employees earning less than 30000.
// Delete employees with low salary
DELETE FROM employees WHERE salary < 30000;
Omitting the WHERE clause deletes every record in the table while keeping the table structure intact.
// Delete all records from employees table
DELETE FROM employees;
This example demonstrates executing a DELETE query using PHP and MySQLi.
// PHP code to delete a record using MySQLi
$conn = mysqli_connect("localhost","root","","company");
$sql = "DELETE FROM employees WHERE employee_id = 5";
mysqli_query($conn,$sql);
If the query executes successfully, the matching row is permanently removed from the database. No output is shown unless you manually display a success or error message in PHP.
Think of DELETE as a filter + eraser. The WHERE clause selects rows, and MySQL erases only those rows. No undo is available unless a backup exists.
SELECT before running DELETETRUNCATE for full-table cleanupDELETE with LIMIT