Best practices for keeping your PAI-OpenCode database healthy and performant.
PAI-OpenCode stores conversation history and session data in a SQLite database at:
~/.opencode/conversations.db
Over time, this database can grow large. This guide explains how to:
- Monitor database health
- Archive old sessions
- Reclaim disk space
- Restore from archives
| Situation | Action |
|---|---|
| DB size > 500MB | Consider archiving sessions > 90 days |
| Sessions > 90 days old | Archive to reduce size |
| Performance slowdown | VACUUM to defragment |
| Pre-upgrade backup | Create archive before migrations |
# Via custom command (in OpenCode chat)
/db-archive
# Via standalone tool
bun Tools/db-archive.ts# Archive sessions older than 90 days (default)
bun Tools/db-archive.ts
# Archive sessions older than 180 days
bun Tools/db-archive.ts 180
# Preview only (no changes)
bun Tools/db-archive.ts --dry-run# 1. Exit OpenCode completely
# 2. Archive first (recommended)
bun Tools/db-archive.ts --vacuumVACUUM rebuilds the database file, reclaiming unused space and defragmenting data.
Archives are stored in:
~/.opencode/archives/
├── sessions-2026-01-15.db
├── sessions-2026-02-28.db
└── .last-archive (timestamp file)
Each archive is a SQLite database containing:
conversationstable (session metadata)messages(serialized as JSON in archive)
Currently, restore requires manual SQL operations:
# 1. Open the archive
sqlite3 ~/.opencode/archives/sessions-2026-01-15.db
# 2. List available sessions
SELECT id, title, updated_at FROM conversations;
# 3. Extract specific session data
SELECT * FROM conversations WHERE id = 'session-id-here';
# 4. Import to live DB (in ~/.opencode/conversations.db)
-- Use INSERT OR REPLACE based on extracted dataThe session-cleanup handler automatically warns you when:
- Database exceeds 500MB
- Sessions older than 90 days exist
Warnings appear at session start (non-blocking).
Add to your crontab for monthly archiving:
# Archive sessions > 180 days monthly
0 2 1 * * cd /path/to/pai-opencode && bun Tools/db-archive.ts 180 >> ~/.opencode/logs/archive.log 2>&1Cause: OpenCode is running and holding the database open.
Solution:
- Exit OpenCode completely
- Retry the operation
Cause: Permissions or disk space.
Solution:
# Check disk space
df -h ~/.opencode
# Check permissions
ls -la ~/.opencode/Cause: Very large database.
Solution:
- Archive sessions first (reduces size)
- Run vacuum during low-activity period
- Ensure 2x current DB size in free disk space
| Feature | Implementation |
|---|---|
| Automatic backup | Created before migration (v2→v3) |
| Archive format | Plain SQLite (queryable) |
| Restore | Manual SQL (for now) |
| Non-destructive | Archiving copies data before removal |
/db-archive— Custom OpenCode commandTools/db-archive.ts— Standalone toolUPGRADE.md— Migration from v2.xCHANGELOG.md— v3.0 features
Part of PAI-OpenCode v3.0 — Personal AI Infrastructure