The DELETE statement is used to remove existing records from a table in MySQL based on a given condition. Use it with caution — without a WHERE clause all rows will be removed.
-- Syntax for DELETE (preview before running)
DELETE FROM table_name WHERE condition;
-- Delete a record from the 'students' table where the name is 'John Doe' (comment)
DELETE FROM students WHERE name = 'John Doe';
-- Delete multiple records where the age is less than 18 (comment)
DELETE FROM students WHERE age < 18;
Matching rows are removed immediately. If you need the ability to undo, perform deletes inside a transaction (if your storage engine supports it) so you can ROLLBACK if necessary. Always preview with SELECT first.
Example preview command:
-- Preview rows that would be deleted to verify before deleting (comment)
SELECT * FROM students WHERE age < 18;
SELECT with the same WHERE clause before deleting to preview affected rows.TRUNCATE to clear an entire table for performance, but note it's not transactional.id.city and preview with a SELECT first.