Imagine you have an employee management system where an employee has resigned, or an e-commerce website where an administrator wants to remove an old product. In these situations, you don’t update the recordโyou permanently remove it from the database.
This is where the SQL DELETE statement is used.
The DELETE statement is one of the four core CRUD operations:
- Create
- Read
- Update
- Delete
After learning INSERT, SELECT, and UPDATE, DELETE is the final step to complete the CRUD cycle.
In this guide, you’ll learn:
- What the DELETE statement is.
- SQL DELETE syntax.
- How to delete records using PHP.
- Why Prepared Statements are important.
- How to add a confirmation dialog before deleting.
- Common mistakes and best practices.
- A complete working example.
What is the SQL DELETE Statement?
The SQL DELETE statement removes one or more records from a database table.
Example:
DELETE FROM users
WHERE id = 5;
This query permanently removes the user whose ID is 5.
Why DELETE is Important?
DELETE is used in almost every web application.
Examples include:
- Removing user accounts
- Deleting blog posts
- Removing products from an online store
- Deleting comments
- Removing orders
- Deleting uploaded files
- Cleaning unwanted records
Without DELETE, your database would keep growing with unnecessary data.
Database Table
Suppose you have the following users table:
| ID | Name | |
|---|---|---|
| 1 | John | john@example.com |
| 2 | David | david@example.com |
| 3 | Sarah | sarah@example.com |
Now suppose David’s account needs to be removed.
SQL DELETE Syntax
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM users
WHERE id = 2;
After executing this query:
| ID | Name |
|---|---|
| 1 | John |
| 3 | Sarah |
David’s record is permanently removed.
โ ๏ธ The Biggest Mistake
Never write:
DELETE FROM users;
This deletes every record in the table.
If your website has 100,000 users, they will all be deleted instantly.
Always specify a WHERE clause:
DELETE FROM users
WHERE id = 2;
This removes only one record.
Golden Rule: Never execute a DELETE query without verifying the
WHEREcondition.