Heuristic:0xK3vin MegaMemory Soft Delete Preserve History
| Knowledge Sources | |
|---|---|
| Domains | Data_Modeling, Knowledge_Graph |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Soft-delete concepts by setting `removed_at` and cascade-deleting incident edges, preserving the node row for audit history and timeline reconstruction.
Description
MegaMemory never hard-deletes concepts during normal operation. Instead, `softDeleteNode` sets `removed_at = datetime('now')` and `removed_reason` on the node, then hard-deletes all incident edges (`DELETE FROM edges WHERE from_id = ? OR to_id = ?`). This keeps the concept row in the database so that:
- Timeline reconstruction can see what existed at any point in time
- The merge engine can detect removal conflicts (one branch removed, other didn't)
- Audit history is preserved with the removal reason
All active-node queries filter by `removed_at IS NULL`, so soft-deleted nodes are invisible to normal operations but remain in the database.
Usage
Apply this heuristic when:
- A concept is no longer accurate or relevant (use `remove_concept` MCP tool)
- You need to track what existed historically for timeline time-travel queries
- Merging branches where one side removed a concept and the other modified it (the merge engine detects this as a conflict)
Do not use hard delete (`hardDeleteNode`) during normal operation — it is reserved for conflict resolution cleanup.
The Insight (Rule of Thumb)
- Action: Use `softDeleteNode(id, reason)` — sets `removed_at` timestamp + `removed_reason` text, then deletes incident edges.
- Value: `removed_at = datetime('now')`, `removed_reason = <user-provided explanation>`. Edges are cascade-hard-deleted.
- Trade-off: Database grows over time (rows are never removed), but timeline fidelity and audit trails are preserved. Edge cleanup prevents stale relationships.
- Compatibility: All `getNode`/`getChildren`/`getRootNodes` queries filter `removed_at IS NULL` automatically. `getNodeIncludingRemoved` is available for merge conflict detection.
Reasoning
Hard-deleting nodes would destroy the ability to reconstruct the graph at any historical timestamp. The timeline feature (`getNodesAtTime`) queries `WHERE created_at <= @timestamp AND (removed_at IS NULL OR removed_at > @timestamp)` — this requires `removed_at` to be preserved.
Edges, however, are hard-deleted on soft-delete because:
- Edges have no `removed_at` column — keeping stale edges would require filtering them in every query
- The merge engine recreates edges from source branches, so no history is lost across merges
- Edge references to removed nodes would break foreign key constraints if kept
The `removed_reason` field ensures the why of the deletion is captured, which is often more valuable than the concept itself for future sessions.
Code Evidence
Soft-delete implementation from `src/db.ts:361-378`:
softDeleteNode(id: string, reason: string): boolean {
let changed = false;
this.runInTransaction(() => {
const stmt = this.db.prepare(`
UPDATE nodes SET removed_at = datetime('now'), removed_reason = @reason, updated_at = datetime('now')
WHERE id = @id AND removed_at IS NULL
`);
const result = stmt.run({ id, reason });
if (result.changes > 0) {
changed = true;
this.db
.prepare("DELETE FROM edges WHERE from_id = ? OR to_id = ?")
.run(id, id);
}
});
return changed;
}
Active node filter in `getNode` from `src/db.ts:299-303`:
getNode(id: string): NodeRow | undefined {
return this.db
.prepare("SELECT * FROM nodes WHERE id = ? AND removed_at IS NULL")
.get(id) as NodeRow | undefined;
}
Time-travel query in `getNodesAtTime` from `src/db.ts:891-897`:
getNodesAtTime(timestamp: string): NodeRow[] {
const stmt = this.db.prepare(`
SELECT * FROM nodes
WHERE created_at <= @timestamp
AND (removed_at IS NULL OR removed_at > @timestamp)
`);
return stmt.all({ timestamp }) as NodeRow[];
}