---
title: "DELETE statement"
description: "Remove rows with the CoraDB DELETE statement: WHERE and LIMIT, multi-table deletes with JOIN or USING, and foreign keys."
url: "https://www.coradb.ai/manual/sql/query/delete.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/query/delete.md"
product: "CoraDB"
version: "1.0"
keywords: "delete statement, delete from, delete limit, delete join, delete using, multi-table delete, foreign key restrict, coradb delete"
---

# DELETE statement

The **DELETE** statement removes rows from one or more tables. A [WHERE clause](https://www.coradb.ai/manual/sql/query/select.html#where-clause) selects the rows to remove; without one, every row of the table is deleted. To empty a whole table quickly, use [TRUNCATE](https://www.coradb.ai/manual/sql/query/truncate.html) instead.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html) and show the real results. They change data, so start corasql with `corasql --no-auto-commit -u dba e-commerce` and finish with `ROLLBACK;` to leave the database as it was.


## Syntax

```text
-- one table
DELETE [FROM] table_name [WHERE <search_condition>] [LIMIT row_count]

-- several tables, FROM form
DELETE table_name [, table_name ...] FROM <table_specifications> [WHERE <search_condition>]

-- several tables, USING form
DELETE FROM table_name [, table_name ...] USING <table_specifications> [WHERE <search_condition>]
```

- *table_name*: the table to delete rows from. With a single table, the `FROM` keyword is optional. In the multi-table forms, list the tables (or their aliases) whose rows are deleted.
- *table_specifications*: the tables to read, written like the `FROM` clause of a [SELECT](https://www.coradb.ai/manual/sql/query/select.html), including joins. Aliases are defined here and can be used in the rest of the statement.
- *search_condition*: only rows that satisfy the condition are deleted.
- *row_count*: the maximum number of rows to delete. It can be an unsigned integer, a host variable or a simple expression. `LIMIT` is allowed only when one table is involved.

## Deleting from one table

Order 455 has three lines:

```sql
SELECT order_id, product_id, quantity, discount_pct
FROM order_items
WHERE order_id = 455
ORDER BY product_id;
```

| order_id | product_id | quantity | discount_pct |
|---:|---:|---:|---:|
| 455 | 123 | 4 | 0 |
| 455 | 335 | 1 | 0 |
| 455 | 1795 | 1 | 25 |

Remove one of them:

```sql
DELETE FROM order_items WHERE order_id = 455 AND product_id = 1795;
```

```text
1 row affected.
```

## Limiting the number of deleted rows

`LIMIT` caps how many of the matching rows are deleted. There are 2,758 single-unit lines without a discount; the statement below deletes only five of them:

```sql
DELETE FROM order_items
WHERE discount_pct = 0 AND quantity = 1
LIMIT 5;
```

```text
5 rows affected.
```

Without `ORDER BY`, which matching rows are removed is not defined, so use `LIMIT` for batch clean-up jobs rather than to pick specific rows.

## Foreign keys

A row that other rows still reference through a foreign key cannot be deleted. Order 455 still has lines in `order_items`, which references `orders`:

```sql
DELETE FROM orders WHERE id = 455;
```

```text
ERROR: Update/Delete operations are restricted by the foreign key 'fk_order_items_order_id'.
```

Delete the referencing rows first, then the parent row, as shown in the next section. The behavior depends on the `ON DELETE` action of the foreign key; see [FOREIGN KEY constraint](https://www.coradb.ai/manual/sql/schema/table_stmt.html#foreign-key-constraint).

## Deleting from several tables

The multi-table forms read rows through a join and delete from the tables you list. Nine orders have not shipped yet, and together they have 29 lines. Remove the lines first:

```sql
DELETE oi
FROM order_items oi
  INNER JOIN orders o ON oi.order_id = o.id
WHERE o.shipped_date IS NULL;
```

```text
29 rows affected.
```

Then the orders themselves:

```sql
DELETE FROM orders WHERE shipped_date IS NULL;
```

```text
9 rows affected.
```

The `USING` form expresses the same join in a different order. This statement deletes the same 29 lines as the first one:

```sql
DELETE FROM oi
USING order_items oi, orders o
WHERE oi.order_id = o.id AND o.shipped_date IS NULL;
```

```text
29 rows affected.
```

Listing both tables in one statement does not get around the foreign key. CoraDB checks the constraint while it deletes, so deleting orders and their lines together fails:

```sql
DELETE oi, o
FROM orders o
  INNER JOIN order_items oi ON oi.order_id = o.id
WHERE o.shipped_date IS NULL;
```

```text
ERROR: Update/Delete operations are restricted by the foreign key 'fk_order_items_order_id'.
```

> **Note:**
> Table aliases can be defined only inside *table_specifications*. Elsewhere in the statement, refer to the tables by those aliases.


## Deleting vertices and edges

`DELETE VERTEX FROM` and `DELETE EDGE FROM` remove rows of [vertex and edge tables](https://www.coradb.ai/manual/sql/schema/table_stmt.html#graph-tables-vertex-and-edge); a plain `DELETE FROM` works too. Deleting a vertex also deletes the edges connected to it, so no edge is left pointing to a missing vertex.

```sql
CREATE VERTEX TABLE warehouse (id INT PRIMARY KEY, name VARCHAR(64));
CREATE EDGE TABLE stocks (quantity INT);
INSERT VERTEX INTO warehouse VALUES (1, 'Bucharest DC'), (2, 'Cluj DC');
INSERT EDGE FROM (SELECT warehouse FROM warehouse WHERE id = 1)
            TO (SELECT product FROM product WHERE id = 1795) INTO stocks VALUES (40);
INSERT EDGE FROM (SELECT warehouse FROM warehouse WHERE id = 2)
            TO (SELECT product FROM product WHERE id = 1795) INTO stocks VALUES (25);

DELETE EDGE FROM stocks WHERE quantity = 25;      -- 1 edge left
DELETE VERTEX FROM warehouse WHERE id = 1;        -- also deletes its edge: 0 edges left
```

## See also

- [TRUNCATE](https://www.coradb.ai/manual/sql/query/truncate.html): empty a table
- [UPDATE](https://www.coradb.ai/manual/sql/query/update.html)
- [WITH (CTE)](https://www.coradb.ai/manual/sql/query/cte.html#ctes-in-other-statements): use a common table expression in a DELETE
- [Transactions](https://www.coradb.ai/manual/sql/transaction.html)
