Concept · Modifying data

DELETE

DELETE removes rows from a table while preserving its structure. WHERE determines which rows; without WHERE, all rows are candidates.

DELETE example

First we look for a row that meets two rules: it is inactive and has no related orders.

Check beforehand
SELECT id, name, active
FROM customers
WHERE active = 0
  AND NOT EXISTS (
    SELECT 1
    FROM orders
    WHERE orders.customer_id = customers.id
  );

On a practice copy, a delete you can undo would look like this:

Practice transaction
BEGIN;

DELETE FROM customers
WHERE active = 0
  AND NOT EXISTS (
    SELECT 1
    FROM orders
    WHERE orders.customer_id = customers.id
  );

SELECT COUNT(*) AS still_exists
FROM customers
WHERE id = 6;

ROLLBACK;

The check returns 0 inside the transaction, and the row reappears after ROLLBACK.

Check the result with small data

We have one active record and one that has been deactivated.

Self-contained test · SQLite
CREATE TABLE demo_deletions (id INTEGER, status TEXT);
INSERT INTO demo_deletions VALUES (1,'active'),(2,'low');
DELETE FROM demo_deletions WHERE status = 'low';
SELECT id, status FROM demo_deletions;
Check result
idstatus
1active

DELETE with WHERE removes only the deactivated record. Before deleting real data, run the same filter in a SELECT and check how many rows it matches.

Check your understanding: What would be the danger of leaving out WHERE?

Show answer

Without WHERE, DELETE would remove every row in the table, including the active record. The statement is valid and the engine gives no warning.

What changes from case to case

A foreign key can block the delete or apply a configured action, such as deleting dependent rows or setting the reference to NULL. Do not assume the behaviour: check the constraint definition before deleting a parent row.

Common mix-ups with DELETE

Running DELETE before measuring its scope

Danger
DELETE FROM orders;

The statement deletes every row. Even if that is the goal, confirm the count, the dependencies, the backup and the engine's transaction behaviour first.

Cleaning up duplicates means classifying rows before deleting them. The guide on removing duplicates while keeping one row shows how to choose which row survives and limit the DELETE to the ids you reviewed.

Check the scope and consequences before deleting

The first question is which rows should stop existing. Reuse the condition in a query that lists their keys and counts the candidates before running the write. A filter on status can catch records from different periods or owners, so add only the limits the real rule requires and review the edge cases.

Look at the database's relationships and actions. Depending on how it is set up, a foreign key can block the delete or cascade it to related rows, and a trigger can have other effects too. The main table losing only a few rows does not mean that is the full impact. A transaction lets you review pending changes where the engine and the operations support it, but it is no substitute for a backup and recovery policy.

Afterwards, check the condition, the number of rows affected and the relationships that must be preserved. If several sessions can change the set between the preview and the delete, consider a transaction with suitable guarantees or a list of keys captured for the operation. Never run a destructive demo against a production database: learning examples should use isolated data and make their unit of work explicit.

Technical questions: SQLite: DELETE (opens in a new tab).