Memory → How to forget (TTL, decay, deletion, user controls)
What it is
Four distinct forgetting mechanisms, each solving a different problem:
| Mechanism | Solves | Implementation |
|---|---|---|
| TTL / expiry | Time-limited facts ("working on the Q3 migration") | expires_at on the record; filtered at retrieval, purged by a job |
| Decay | Gradual irrelevance | Recency weighting at retrieval (when to retrieve memory) plus eviction of never-retrieved, low-score entries |
| Supersession | Facts that changed | Mark old as superseded_by, keep for audit, exclude from retrieval (memory conflict resolution) |
| Explicit deletion | Wrong facts, user requests, legal erasure | Hard delete across every store, with verification |
The last one is the one most systems get wrong, because a memory doesn't live in one place: it's in the relational store, the vector index, the derived summaries (compaction / summarisation of long history), the trace logs (observability & tracing), backups, caches, and any eval sets it was sampled into. "Delete" must mean all of them.
How to implement
- Every memory gets a lifecycle:
created_at,expires_at(nullable),last_retrieved_at,retrieval_count,superseded_by,deleted_at. - Retrieval filters exclude expired, superseded, and soft-deleted records — always, in one shared query builder.
- TTL by memory kind: task context days, episodes months, preferences indefinite. Set at write time (what to write to memory).
- Eviction policy when a user's store hits a cap: drop lowest
score × recency × retrieval_countfirst. - User-facing controls: list memories, correct a value, delete one, delete all — as a real product surface, not an admin script.
- Erasure across stores: a
forget(user_id, memory_id | all)operation that removes from the relational store, the vector index, derived summaries, caches, and marks trace records for redaction; with a completion record. - Verify: a post-deletion check that queries every store for the identifier and asserts zero results.
Small example
from datetime import datetime, timedelta, timezone
TTL_BY_KIND = {"task_context": timedelta(days=14), "episodic": timedelta(days=180),
"semantic": None, "procedural": None} # None = no automatic expiry
def active_memory_filter(user_id: str, tenant_id: str) -> dict:
"""One shared filter — used by every retrieval path so nothing forgotten can resurface."""
now = datetime.now(timezone.utc)
return {"must": [{"key": "user_id", "match": {"value": user_id}},
{"key": "tenant_id", "match": {"value": tenant_id}},
{"key": "deleted_at", "is_null": True},
{"key": "superseded_by", "is_null": True}],
"must_not": [{"key": "expires_at", "range": {"lt": now.timestamp()}}]}
def expire_and_evict(conn, user_id, max_memories=200):
now = datetime.now(timezone.utc)
conn.execute("""UPDATE memories SET deleted_at=now(), delete_reason='expired'
WHERE user_id=%s AND expires_at < %s AND deleted_at IS NULL""", (user_id, now))
over = conn.execute("""SELECT id FROM memories WHERE user_id=%s AND deleted_at IS NULL
ORDER BY (confidence * pow(0.5, EXTRACT(EPOCH FROM (now()-created_at))/86400/30)
* (1 + retrieval_count)) ASC
OFFSET %s""", (user_id, max_memories)).fetchall()
for (mid,) in over:
forget_memory(conn, user_id, mid, reason="evicted")
def forget_memory(conn, user_id: str, memory_id: str | None = None, reason: str = "user_request") -> dict:
"""Erase from EVERY store. memory_id=None means erase all memories for the user."""
op_id = str(uuid.uuid4())
audit_log(action="memory_erasure_start", op_id=op_id, user_id=user_id, memory_id=memory_id, reason=reason)
ids = ([memory_id] if memory_id else
[r[0] for r in conn.execute("SELECT id FROM memories WHERE user_id=%s", (user_id,)).fetchall()])
with conn.transaction(): # 1) system of record
conn.execute("UPDATE memories SET deleted_at=now(), delete_reason=%s, value=NULL "
"WHERE id = ANY(%s)", (reason, ids))
vec.delete(collection="memories", ids=ids) # 2) vector index (hard delete)
summary_store.rebuild_for_user(user_id, exclude_ids=ids) # 3) derived summaries (compaction / summarisation of long history)
cache.delete_prefix(f"memory:{user_id}:") # 4) caches
trace_store.mark_for_redaction(user_id=user_id, memory_ids=ids) # 5) traces/logs (data residency & retention)
eval_store.remove_samples(user_id=user_id, memory_ids=ids) # 6) anything sampled into eval sets
verification = verify_erasure(user_id, ids) # 7) prove it
audit_log(action="memory_erasure_complete", op_id=op_id, n=len(ids), verified=verification["clean"])
if not verification["clean"]:
alert_erasure_incomplete(op_id, verification["found_in"])
return {"op_id": op_id, "erased": len(ids), "verified": verification["clean"]}
def verify_erasure(user_id, ids) -> dict:
found = []
if conn.execute("SELECT 1 FROM memories WHERE id=ANY(%s) AND value IS NOT NULL", (ids,)).fetchone():
found.append("relational")
if vec.retrieve(collection="memories", ids=ids): found.append("vector")
if cache.get_any(f"memory:{user_id}:"): found.append("cache")
return {"clean": not found, "found_in": found}
# --- user-facing surface ---
def list_user_memories(user_id, tenant_id):
return [{"id": m.id, "kind": m.kind, "field": m.field, "value": m.value,
"source": m.source, "learned": m.created_at.date().isoformat(),
"last_used": m.last_retrieved_at, "expires": m.expires_at}
for m in query_memories(active_memory_filter(user_id, tenant_id))]
Pros
- Wrong facts don't persist forever — the single biggest quality risk of memory systems.
- Legal compliance: GDPR/CCPA erasure requests are answerable, with proof (data residency & retention).
- Bounded store size keeps retrieval precise and cost predictable.
- User trust: a visible, editable memory list turns an unsettling feature into a controllable one.
- Supersession preserves an audit trail while removing the fact from use.
Cons
- Erasure across stores is genuinely hard — vector indexes, derived summaries, caches, logs, backups, and eval sets all hold copies.
- Backups are the awkward case: most organisations rely on retention windows rather than surgical backup deletion; that policy must be documented and defensible.
- Over-aggressive TTL loses useful context, making the system feel forgetful.
- User-facing controls are product work — a list, an edit, a delete, and an explanation.
- Deleting memories used in past answers can make audit logs inconsistent (keep the answer's provenance separately).
When to use
Always. A memory system without deletion is a compliance liability and a quality time bomb.
When NOT to use (aggressive expiry)
- Stable preferences (role, region, communication style) — no TTL; they change by supersession, not expiry.
- Regulated contexts requiring retention of interaction records — separate the audit record (retained) from the active memory (erasable).
Production at scale
Design erasure before you design writes. Retrofitting deletion onto a memory system with derived summaries, caches, and vector indexes is far harder than building the forget() path from the start. Enumerate every store a memory can reach and make deletion cover all of them.
One shared retrieval filter for active memories. If any code path builds its own query, a forgotten memory will resurface — and it will do so in front of the user who asked you to delete it.
Verification, not hope. After deletion, query every store and assert absence. Log the verification result and alert on failures. "We ran the delete" is not the same as "it's gone".
Backups and logs need a documented policy: typically, backups age out within a defined window and log redaction is applied on a schedule. Write it down, make legal review it, and be able to state it to a customer.
Separate audit records from memory. A past answer's provenance (what was retrieved and shown) is an audit artifact with its own retention; the active memory is separately erasable. Conflating them makes erasure impossible or auditing impossible.
Tenant-level erasure: when a customer leaves, all memories for all their users must go. Test this path; it's the one that appears in contracts.
Expose the controls prominently. Users who can see what's remembered correct it; users who can't, distrust it. Correction volume is also a quality signal for the write gate (what to write to memory).
Retention SLA: define and measure time from request to verified erasure (often contractually 30 days, sometimes much less). Dashboard it.
Prod checklist
- Lifecycle fields on every memory: created, expires, superseded, deleted, last retrieved, retrieval count
- One shared active-memory filter used by all retrieval paths
- TTL per memory kind set at write time; expiry job running
- Eviction policy with a per-user cap
-
forget(user, memory|all)covering relational, vector, summaries, caches, traces, eval sets - Post-deletion verification across all stores, with alerting on failure
- Backup and log retention policy documented and legally reviewed
- Audit records separated from erasable active memory
- Tenant-level erasure path tested
- User-facing list / correct / delete / delete-all controls
- Erasure SLA measured from request to verified completion
Metrics to watch
| Metric | Why |
|---|---|
| Time from erasure request to verified completion | Compliance SLA |
| Erasure verification failures (must be 0) | Incomplete deletion |
| User correction and deletion rate | Write-gate quality signal |
| Memories expired / evicted per period | Lifecycle working |
| Store size per user over time | Growth control |
Related
- What to write — fewer writes, less to forget
- Conflict resolution — supersession
- Compaction — derived summaries hold copies
- Data residency & retention, Auditability