Skip to content

Conversation

@lsvishaal
Copy link
Contributor

Description

This PR implements automatic graph cleanup when memories are deleted, addressing Issue #3245. Previously, when a memory was deleted from the vector store, the associated entities and relationships in the graph database remained as orphaned nodes, leading to data inconsistency and potential memory leaks.

The solution adds a delete() method to all graph store implementations (Neo4j, Memgraph, Kuzu, and Neptune) that properly cleans up entity relationships by:

  • Re-extracting entities from the deleted memory content
  • Decrementing the mentions counter for each entity
  • Removing entities when their mention count reaches zero
  • Cleaning up orphaned nodes and relationships

The implementation includes error handling to ensure memory deletion continues even if graph cleanup fails, maintaining system reliability.

Fixes #3245

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Tests

Test Coverage:

  • Added test_delete_memory_calls_graph_cleanup - Verifies that graph.delete() is called with correct parameters when graph is enabled
  • Added test_delete_memory_continues_without_graph - Ensures deletion works when graph is not enabled
  • All existing tests pass (9/9 in 0.89s)

Test Configuration:

  • Python 3.12.4
  • pytest 8.4.2
  • All graph backends covered: Neo4j, Memgraph, Kuzu, Neptune (NeptuneDB & NeptuneGraph)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@lsvishaal lsvishaal closed this Oct 9, 2025
@lsvishaal lsvishaal reopened this Oct 9, 2025
- Implement delete() method for Neo4j, Memgraph, Kuzu, and Neptune
- Extract entities from deleted memory and decrement mentions counter
- Remove orphaned nodes when mention count reaches zero
- Add comprehensive tests for graph cleanup with/without graph enabled
- Add type hints (tuple[str, dict]) to Neptune _delete_entities_cypher
- Ensure memory deletion is security-scoped (user_id/agent_id/run_id)

Resolves mem0ai#3245
@lsvishaal lsvishaal force-pushed the fix/neo4j-cleanup-on-delete-3245 branch from 32be873 to befd3db Compare October 9, 2025 20:11
@lsvishaal
Copy link
Contributor Author

lsvishaal commented Oct 9, 2025

apologies for the mess! 😓, I squashed the commits into a single clean commit for easier review.

@parshvadaftari
Copy link
Contributor

Hey @lsvishaal no worries, will get this PR reviewed in some time. Thanks for implementing it

@lsvishaal
Copy link
Contributor Author

Hey @parshvadaftari , any updates? 😁

Copy link
Contributor

@parshvadaftari parshvadaftari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest looks good to me. Please incorporate the requested changes

@patch('mem0.utils.factory.VectorStoreFactory.create')
@patch('mem0.utils.factory.LlmFactory.create')
@patch('mem0.memory.storage.SQLiteManager')
def test_delete_memory_calls_graph_cleanup(mock_sqlite, mock_llm_factory, mock_vector_factory, mock_embedder_factory, mock_graph_factory):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests which cover entire end to end graph deletion? and move the graph related tests to separate file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,
Added end to end tests in separate file as requested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parshvadaftari
Hi, any updates?

- Added E2E tests for Graph Memory Deletion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory deletion does not clean up Neo4j graph data

2 participants