Skip to content

Commit 2950bb9

Browse files
phernandezclaude
andcommitted
fix: improve reset command reliability and UX
- Skip database initialization for reset command (like mcp/status/etc) - Delete WAL files (-shm, -wal) along with main database file - Add user-friendly error messages when database is locked - Show reassuring message that note files won't be deleted 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: phernandez <[email protected]>
1 parent 3f8a800 commit 2950bb9

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/basic_memory/cli/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ def app_callback(
6363
# Run initialization for commands that don't use the API
6464
# Skip for 'mcp' command - it has its own lifespan that handles initialization
6565
# Skip for API-using commands (status, sync, etc.) - they handle initialization via deps.py
66-
api_commands = {"mcp", "status", "sync", "project", "tool"}
66+
# Skip for 'reset' command - it manages its own database lifecycle
67+
skip_init_commands = {"mcp", "status", "sync", "project", "tool", "reset"}
6768
if (
6869
not version
6970
and ctx.invoked_subcommand is not None
70-
and ctx.invoked_subcommand not in api_commands
71+
and ctx.invoked_subcommand not in skip_init_commands
7172
):
7273
from basic_memory.services.initialization import ensure_initialization
7374

src/basic_memory/cli/commands/db.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,64 @@
44

55
import typer
66
from loguru import logger
7+
from rich.console import Console
8+
from sqlalchemy.exc import OperationalError
79

810
from basic_memory import db
911
from basic_memory.cli.app import app
1012
from basic_memory.config import ConfigManager, BasicMemoryConfig, save_basic_memory_config
1113

14+
console = Console()
15+
1216

1317
@app.command()
1418
def reset(
1519
reindex: bool = typer.Option(False, "--reindex", help="Rebuild db index from filesystem"),
1620
): # pragma: no cover
1721
"""Reset database (drop all tables and recreate)."""
18-
if typer.confirm("This will delete all data in your db. Are you sure?"):
22+
console.print(
23+
"[yellow]Note:[/yellow] This only deletes the index database. "
24+
"Your markdown note files will not be affected."
25+
)
26+
if typer.confirm("Reset the database index? (You can rebuild it with 'bm sync')"):
1927
logger.info("Resetting database...")
2028
config_manager = ConfigManager()
2129
app_config = config_manager.config
2230
# Get database path
2331
db_path = app_config.app_database_path
2432

25-
# Delete the database file if it exists
26-
if db_path.exists():
27-
db_path.unlink()
28-
logger.info(f"Database file deleted: {db_path}")
33+
# Delete the database file and WAL files if they exist
34+
for suffix in ["", "-shm", "-wal"]:
35+
path = db_path.parent / f"{db_path.name}{suffix}"
36+
if path.exists():
37+
try:
38+
path.unlink()
39+
logger.info(f"Deleted: {path}")
40+
except OSError as e:
41+
console.print(
42+
f"[red]Error:[/red] Cannot delete {path.name}: {e}\n"
43+
"The database may be in use by another process (e.g., MCP server).\n"
44+
"Please close Claude Desktop or any other Basic Memory clients and try again."
45+
)
46+
raise typer.Exit(1)
2947

3048
# Reset project configuration
3149
config = BasicMemoryConfig()
3250
save_basic_memory_config(config_manager.config_file, config)
3351
logger.info("Project configuration reset to default")
3452

3553
# Create a new empty database
36-
asyncio.run(db.run_migrations(app_config))
54+
try:
55+
asyncio.run(db.run_migrations(app_config))
56+
except OperationalError as e:
57+
if "disk I/O error" in str(e) or "database is locked" in str(e):
58+
console.print(
59+
"[red]Error:[/red] Cannot access database. "
60+
"It may be in use by another process (e.g., MCP server).\n"
61+
"Please close Claude Desktop or any other Basic Memory clients and try again."
62+
)
63+
raise typer.Exit(1)
64+
raise
3765
logger.info("Database reset complete")
3866

3967
if reindex:

0 commit comments

Comments
 (0)