-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Problem
When a memory is deleted via the API (DELETE /v3/memories/{memory_id}), the knowledge graph is not updated. This leaves orphaned references and stale data in the graph.
Currently:
- ✅ Memory is deleted from the
memoriescollection - ❌ Knowledge graph nodes/edges still contain the deleted
memory_idin theirmemory_idsarrays - ❌ Nodes/edges remain in the graph even if they were only derived from deleted memories
Impact:
- Graph contains stale entities that no longer have valid source memories
memory_idsarrays contain invalid references- Graph becomes progressively "dirty" as users delete memories
- Only way to fix currently is a full rebuild (
POST /v1/knowledge-graph/rebuild)
Expected Behavior
When a memory is deleted, the knowledge graph should automatically clean itself:
- Remove the memory ID from all nodes — scan nodes and remove the deleted
memory_idfrom theirmemory_idsarray - Remove the memory ID from all edges — scan edges and remove the deleted
memory_idfrom theirmemory_idsarray - Delete orphaned nodes — if a node's
memory_idsarray becomes empty after cleanup, delete the node - Delete orphaned edges — if an edge's
memory_idsarray becomes empty, OR if either its source/target node was deleted, delete the edge
Files to Modify
backend/routers/memories.py—delete_memory()endpointbackend/database/knowledge_graph.py— may need new helper functions for cleanup
Considerations
- This should also apply to
DELETE /v3/memories(bulk delete all memories) — in that case, the entire knowledge graph should be cleared - Consider whether this cleanup should be synchronous or run as a background task (for performance)
- Edge case: if a node/edge has multiple
memory_ids, only remove the one being deleted, don't delete the node/edge itself
Acceptance Criteria
- Deleting a single memory removes its ID from all graph nodes and edges
- Nodes with empty
memory_idsare automatically deleted - Edges with empty
memory_idsor missing source/target nodes are automatically deleted - Bulk memory delete clears the entire knowledge graph
- No orphaned references remain after memory deletion
Metadata
Metadata
Assignees
Labels
No labels