fix: Prevent deleted projects from being recreated by background sync (#193) by phernandez · Pull Request #370 · basicmachines-co/basic-memory · GitHub
Skip to content

fix: Prevent deleted projects from being recreated by background sync (#193) - #370

Merged
phernandez merged 1 commit into
mainfrom
fix/prevent-deleted-project-recreation-193
Oct 16, 2025
Merged

fix: Prevent deleted projects from being recreated by background sync (#193)#370
phernandez merged 1 commit into
mainfrom
fix/prevent-deleted-project-recreation-193

Conversation

@phernandez

Copy link
Copy Markdown
Member

Summary

Fixes issue #193 from basic-memory-cloud where deleted projects would reappear after being removed by the user. This issue affected both local and cloud deployments.

Root Cause

When a project was deleted, the directory still existed on disk, and two different mechanisms would resurrect it:

  1. Background sync (watch_service): Would continue processing file changes in the deleted project's directory, causing database entries to be recreated
  2. Project synchronization: Would re-add projects from the database back to the configuration file, treating the database as the source of truth when it should be the config file

Evidence from Cloud

Timeline from Logfire for tenant 0a20eb58-970f-ab05-ff49-25a9cdb2179c:

  • 09:01:10 - DELETE /projects/chris-ledoux-personal-info → 200 OK (deleted successfully)
  • 10:21:40 - DELETE /projects/chris-ledoux-personal-info → 400 Bad Request (project exists again!)
  • 10:22:01 - DELETE /projects/chris-ledoux-personal-info → 200 OK (deleted again)

The cloud sync worker runs reconcile_projects_with_config() every 30 seconds, which was calling synchronize_projects() and re-adding the deleted project back to config from the database.

Changes Made

src/basic_memory/sync/watch_service.py (lines 241-251):

  • Added configuration check in handle_changes() before processing file changes
  • If project doesn't exist in config, skip sync to prevent recreation
  • Logs: "Skipping sync for deleted project"

src/basic_memory/services/project_service.py (lines 363-371):

  • Changed synchronize_projects() behavior for DB-only projects
  • Before: Added projects from DB to config (bidirectional sync)
  • After: Removes projects from DB that don't exist in config (config is source of truth)
  • Logs: "Removing project '{name}' from database (deleted from config, source of truth)"

Tests Added:

  • test_handle_changes_skips_deleted_project in tests/sync/test_watch_service.py
  • test_synchronize_projects_removes_db_only_projects in tests/services/test_project_service.py

Impact

This fix ensures that:

  1. Config file is the single source of truth for project existence
  2. Deleted projects stay deleted even when their directories remain on disk
  3. Background sync workers in the cloud won't resurrect deleted projects
  4. The reconcile_projects_with_config() function now properly cleans up orphaned DB entries

Test Plan

  • Both new tests pass
  • All existing watch service tests pass (15 tests)
  • All existing project service tests pass (35+ tests)
  • Fix validates config as source of truth for both local and cloud scenarios

🤖 Generated with Claude Code

…#193)

This fix addresses issue #193 where deleted projects would reappear after being
removed by the user. The problem occurred in two places:

1. **watch_service.handle_changes()**: Background sync would continue processing
   file changes for projects that had been deleted from configuration, causing
   database entries to be recreated for orphaned directories.

2. **project_service.synchronize_projects()**: Project synchronization would
   re-add projects from the database back to the configuration file, treating
   the database as the source of truth when it should be the config file.

Root cause: When a project was deleted, the directory still existed on disk,
and two different mechanisms would resurrect it:
- Background sync processing file changes in the deleted project's directory
- Project synchronization adding DB-only projects back to config

Changes:
- watch_service.py: Added check if project exists in config before syncing
- project_service.py: Changed synchronize_projects to remove DB-only projects
  instead of adding them to config (config is source of truth)
- test_watch_service.py: Added test_handle_changes_skips_deleted_project
- test_project_service.py: Added test_synchronize_projects_removes_db_only_projects

This fix ensures config is the single source of truth for project existence,
and deleted projects stay deleted even when their directories remain on disk.

Signed-off-by: Claude <noreply@anthropic.com>
Signed-off-by: phernandez <paul@basicmachines.co>
@github-actions

github-actions Bot commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

@phernandez
phernandez merged commit 449b62d into main Oct 16, 2025
16 checks passed
@phernandez
phernandez deleted the fix/prevent-deleted-project-recreation-193 branch October 16, 2025 20:16
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.

1 participant