fix(graph): soft-delete graph relationships instead of hard DELETE#4188
Merged
kartik-mem0 merged 4 commits intomem0ai:mainfrom Mar 21, 2026
Merged
Conversation
Replace hard DELETE in _delete_entities with SET r.valid = false to enable temporal reasoning over historical graph state, aligning the implementation with the paper's described architecture (arXiv:2504.19413). - _delete_entities: SET r.valid = false, r.invalidated_at = datetime() - _search_graph_db: filter WHERE r.valid IS NULL OR r.valid = true - get_all: same filter for consistency - delete_all: unchanged (explicit user action keeps DETACH DELETE) The IS NULL check ensures backward compatibility with existing edges that lack the valid property. Note: For large graphs with frequent updates, consider adding a Neo4j index on the valid property for relationship types that are frequently queried: CREATE INDEX rel_valid FOR ()-[r:RELATIONSHIP]-() ON (r.valid) Fixes mem0ai#4187 Signed-off-by: sxu75374 <imshuaixu@gmail.com>
kartik-mem0
approved these changes
Mar 21, 2026
lukaj99
added a commit
to lukaj99/mem0
that referenced
this pull request
Mar 21, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
…em0ai#4188) Signed-off-by: sxu75374 <imshuaixu@gmail.com> Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Replaces hard
DELETE rin graph memory's conflict resolution with soft-delete (SET r.valid = false), aligning the implementation with the architecture described in the Mem0 paper (arXiv:2504.19413).Fixes #4187
Changes
graph_memory.py:_delete_entitiesnow usesSET r.valid = falseinstead ofDELETE rgraph_memory.py: Search andget_allqueries filter onWHERE r.valid IS NULL OR r.valid = true— backward compatible with existing edges that don't have thevalidpropertytest_graph_memory_soft_delete.py: 8 tests covering Cypher generation, search filters, get_all filters, backward compat, and idempotencyWhy
The current hard
DELETEpermanently removes graph edges, making temporal reasoning impossible. When importing historical data, conflict detection can corrupt current state by deleting newer edges and replacing them with older information.Soft-delete preserves edge history and enables future temporal queries via
include_historicalparameter.Backward Compatibility
Uses
r.valid IS NULL OR r.valid = trueso existing edges without thevalidproperty continue to appear in queries. No migration needed.Signed-off-by: sxu75374 imshuaixu@gmail.com