Skip to content

DELETE ​

The DELETE statement removes rows matching an optional WHERE predicate from a table, enforcing foreign-key referential actions and updating all table indexes.

Syntax ​

sql
DELETE FROM table_name
[WHERE filter_condition];

Parameters ​

ParameterDescription
table_nameTarget table from which rows will be removed.
WHERE filter_conditionOptional predicate selecting which rows to delete. When omitted, all rows in the table are deleted.

Filtered DELETE ​

Provide a WHERE clause to remove only rows matching your condition. WHERE supports comparisons, logical operators, IN, BETWEEN, LIKE/NOT LIKE, IS [NOT] NULL, subqueries, and vector distance predicates.

sql
-- Delete a single row by primary key
DELETE FROM players
WHERE Id = 4;

-- Delete with compound predicates
DELETE FROM matches
WHERE PlayedOn < '2025-01-01'
  AND Points = 0;

-- Delete using a subquery
DELETE FROM players
WHERE NOT EXISTS (
    SELECT 1
    FROM matches m
    WHERE m.PlayerId = players.Id
);

Full-Table DELETE ​

Omitting the WHERE clause removes every row in the table while preserving the table schema, constraints, and index definitions.

sql
DELETE FROM matches;

Destructive Operation

DataVo v0.4 does not have a TRUNCATE TABLE statement; DELETE FROM table_name; removes every row and executes through normal constraint, index, and transaction paths. Wrap bulk deletes inside BEGIN ... COMMIT if you need an undo boundary.

Foreign Keys & Index Maintenance ​

  • Referential actions: Deleting a parent row checks all referencing child tables. ON DELETE CASCADE removes dependent child rows automatically; ON DELETE RESTRICT rejects the DELETE if any child row still references the target key.
  • Automatic index cleanup: Removed rows are purged from all B-Tree, HNSW, FLAT, and COLUMNAR indexes on the table.
  • Unsupported clauses: DELETE ... USING joins and RETURNING clauses are not supported in DataVo v0.4.

Transactions & Rollback Behavior ​

Inside an explicit transaction (BEGIN ... COMMIT), DELETE does not immediately destroy committed rows on disk. Instead, deletes are buffered in TransactionContext:

  • Read-your-own-writes: Subsequent SELECT, UPDATE, and DELETE statements inside the same transaction immediately exclude the deleted rows.
  • Buffered inserts: If a row was inserted earlier in the same uncommitted transaction, DELETE drops it from the transaction's insert buffer.
  • Full reversibility: Executing ROLLBACK or ROLLBACK TO SAVEPOINT cleanly discards the buffered deletes so committed rows remain untouched.
sql
BEGIN;

DELETE FROM players WHERE Region = 'Legacy';
SAVEPOINT after_cleanup;

DELETE FROM players WHERE Score < 200;

-- Undo only the second delete
ROLLBACK TO SAVEPOINT after_cleanup;

COMMIT;

Reclaiming Space with VACUUM ​

In legacy Disk storage mode (or when manually compacting table storage after heavy deletes), deleted rows remain as tombstones in the physical table file until compacted. Run VACUUM on a table to reclaim that space:

sql
VACUUM players;

VACUUM table_name performs three steps:

  1. Scans the table and strips all tombstoned (soft-deleted) rows.
  2. Rewrites the surviving rows into a compacted table file with fresh, contiguous internal row IDs.
  3. Drops and rebuilds all B-Tree indexes on the table against the new row IDs.

When You Need VACUUM

The default Lsm storage mode compacts deleted keys automatically during background/flush merges. VACUUM <table> is primarily intended for manual space reclamation in legacy Disk mode or after large one-time purges.

See Also ​

  • INSERT: Adding new rows to a table
  • UPDATE: Modifying existing rows
  • CREATE TABLE: Configuring ON DELETE CASCADE and ON DELETE RESTRICT foreign keys
  • Transactions: Using BEGIN, SAVEPOINT, ROLLBACK TO SAVEPOINT, and COMMIT