DELETE removes one or many rows from a table based on an optional WHERE filter.
DELETE permanently removes rows from a table. When no WHERE clause is provided, all rows are deleted. Adding WHERE, JOIN, LIMIT, or ORDER BY lets you target specific records.
Use DELETE FROM followed by the table name and an optional WHERE clause.LIMIT and ORDER BY control how many and which rows are deleted.
DELETE FROM Orders WHERE id = 42; removes only the order whose primary key equals 42.
DELETE FROM Orders WHERE order_date < '2023-01-01'; quickly clears out legacy orders created before 2023.
DELETE oi FROM OrderItems oi JOIN Products p ON p.id = oi.product_id WHERE p.stock = 0; removes items linked to out-of-stock products.
Run SELECT * FROM Orders WHERE total_amount = 0; first.Once confirmed, swap SELECT with DELETE. This prevents accidental mass deletion.
Always index columns in the WHERE clause, delete in small batches with LIMIT, disable foreign-key checks only when necessary, and monitor transaction size.
Missing WHERE: Deletes all rows—always double-check. Ignoring foreign keys: May block deletion—add ON DELETE CASCADE or delete child rows first.
.
Only if the DELETE was run inside a transaction that has not yet been committed. Otherwise, you must restore from backup.
Yes, TRUNCATE instantly removes all rows and resets auto-increment values, but you cannot filter rows and it requires elevated privileges.
Add ORDER BY id LIMIT 1000 to your DELETE and loop until no rows remain. This keeps transactions small and locks minimal.