Skip to content

DELETE

Description

The DELETE FROM statement soft-deletes matching rows from a table. In MemoraDB, a deletion does not erase the row from disk; instead, it appends a tombstone record (deleted = 1) to data.db. The row is excluded from standard queries but remains preserved for historical and temporal inspection.


Syntax

(See the Syntax Notation Guide for notation rules).

Basic syntax

DELETE FROM <table> WHERE <condition>;

Full syntax

DELETE FROM <table> [WHERE <condition>];

Parameters

Parameter Description
<table> The target table name.
WHERE <condition> Optional filter condition. If omitted, every active row in the table is deleted.

Examples

1. Delete Specific Row by Primary Key (students)

DELETE FROM students WHERE id = 1;

2. Delete with Comparison Condition (students)

DELETE FROM students WHERE gpa < 2.0;

3. Delete All Rows (students)

DELETE FROM students;

Expected Output

1 row(s) deleted

How It Works

  1. Target Selection: The executor scans the current active records (table->scanLatest()).
  2. WHERE Filtering: If a WHERE clause is provided, matching records are identified.
  3. Tombstone Append:
  4. For each target record, table->deleteRow(pk) is invoked.
  5. A new record is appended to the end of data.db with the deleted flag set to 1.
  6. The new version {timestamp, offset} is added to HistoryIndex.
  7. Subsequent Query Behavior:
  8. Standard SELECT * FROM table; ignores tombstoned records.
  9. SELECT * FROM table AS OF <past_date>; will still return the record if it was active at <past_date>.
  10. HISTORY table WHERE pk = <val>; displays the complete lifecycle, including the tombstone event.

Errors & Edge Cases

Error Condition Error Message
Table Does Not Exist No such table: '<table>'
Unknown Column in WHERE Unknown column '<col>' on table '<table>'
Primary Key Not Found If no active rows match the condition, 0 row(s) deleted is returned.

  • INSERT — Insert records.
  • UPDATE — Update records.
  • HISTORY — View deletion tombstones in row history.
  • ROLLBACK — Restore deleted rows back to life.