Skip to main content

Safely analyze and vacuum OpenCode's SQLite database

Project description

ocdbc — OpenCode Database Cleaner

Safely analyze and shrink OpenCode's SQLite database.

OpenCode stores all session history in a single SQLite database (~/.local/share/opencode/opencode.db). Over time, compaction and session archival leave behind freelist pages — free space inside the file that is never returned to the filesystem unless auto_vacuum is enabled and VACUUM is run. Databases routinely bloat to 1–2 GB, with >50% being dead air.

ocdbc provides two commands:

  • analyze — read-only health report (no risk)
  • vacuum — safe VACUUM sequence with backup, integrity checks, and auto_vacuum = INCREMENTAL enablement

Installation

git clone https://github.com/chncaesar/opencode-db-clean.git
cd opencode-db-clean
chmod +x ocdbc
# optional: symlink into PATH
ln -s "$(pwd)/ocdbc" ~/.local/bin/ocdbc

Requires Python 3.8+ (stdlib only, zero pip dependencies).

Usage

Analyze (always safe, read-only)

ocdbc analyze

Shows: DB size, live data vs freelist, table sizes, session age distribution, and the 5 largest messages.

Vacuum (safe sequence)

ocdbc vacuum

The vacuum command performs these steps in order:

  1. fuser check — refuse if OpenCode still has the DB open (unless --skip-fuser)
  2. WAL checkpoint — flush write-ahead log into the main DB
  3. Integrity check — abort if database is corrupt
  4. Backup — timestamped copy, then verify backup integrity
  5. Enable auto_vacuum = INCREMENTAL — prevent future freelist bloat
  6. VACUUM — rebuild the database with progress indication
  7. Re-enable WALVACUUM resets journal mode; restore it
  8. Final integrity check — verify the result
# Skip the confirmation prompt
ocdbc vacuum --force

# Skip backup (dangerous — backup is recommended)
ocdbc vacuum --no-backup --force

# Proceed without fuser installed (use with caution)
ocdbc vacuum --skip-fuser --force

# Custom database path
ocdbc vacuum --path /custom/path/opencode.db

Safety

  • Refuses to run if OpenCode has the database open (detected via fuser)
  • Refuses to run if fuser is not installed — pass --skip-fuser to override
  • Checks integrity before and after — abort if anything looks wrong
  • Creates a verified timestamped backup before VACUUM (unless --no-backup)
  • VACUUM uses atomic rename — original DB untouched if the operation fails

Example

$ ocdbc analyze

OpenCode Database Health Report
──────────────────────────────────────────────────────────
  Path:          /home/zjc/.local/share/opencode/opencode.db
  File size:     1.3 GB
  Page size:     4.0 KB
  Journal mode:  wal
  Auto-vacuum:   OFF (NONE)

Storage
──────────────────────────────────────────────────────────
  Live data:     525 MB
  Freelist:      766 MB  (56% of file)
   VACUUM would reclaim ~766 MB
   Estimated result:  ~525 MB
$ ocdbc vacuum

   No process has the database open

  Current size:   1.3 GB
  Live data:      525 MB
  Freelist:       766 MB (56%)

  Proceed with VACUUM? [y/N] y

   Integrity check passed
   Backup created (1.3 GB)
   WAL checkpointed
   auto_vacuum set to INCREMENTAL
   VACUUM complete
   journal_mode = wal
   Integrity check passed

Results
──────────────────────────────────────────────────────────
  Before:   1.3 GB
  After:    534 MB
  Reclaimed: 766 MB

  Backup:   /home/zjc/.local/share/opencode/opencode.db.backup.2026-07-22T...
  (Keep it until you verify OpenCode works correctly)

Related

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ocdbc-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ocdbc-0.1.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file ocdbc-0.1.0.tar.gz.

File metadata

  • Download URL: ocdbc-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for ocdbc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2dad30d96fd3c47ba32d8c249ba19637068afd328b564a364ae9278fe846c3b5
MD5 c4854561650f2af361a26ec50c1bd084
BLAKE2b-256 c6cc71c8fcf1bd423a5c8fb8b299f41ae0c75a6097faa1d8cb1be1bf6a187efc

See more details on using hashes here.

File details

Details for the file ocdbc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ocdbc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for ocdbc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4950d43509b5857a88d7912523a2383f230247f349c63c5a13c5da03ab39601b
MD5 5859d62cf8e61b717759201cdd9f9463
BLAKE2b-256 00f3bc71d68459862a0560c7dad9c0bef8ab698f4e6a018115ee76ba9fd09bd9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page