anxkhn/opencode-vacuum

A /vacuum slash command for OpenCode: interactively prune old sessions and shrink the local SQLite database.

★ 1Forks 0JavaScriptGitHub ↗Compare

README

opencode-vacuum

A tiny OpenCode plugin that adds a /vacuum slash command. It opens a settings-style panel where you dial in prune rules (age, size, keep-per-folder), see live how many sessions match, then hit Clear & prune to delete them and run VACUUM to shrink the database file on disk.

Why: OpenCode stores every session as an append-only event log (~/.local/share/opencode/opencode.db). Streaming responses write full message snapshots on each update, so the event table grows over time and is only reclaimed when sessions are deleted. /vacuum lets you clear out the sessions you no longer need, then reclaims the space.

OpenCode Vacuum  -  999.5 MB  -  delete 3/229 (~222.4 MB)

  Rules
    Older than        off
    Larger than       50 MB
    Keep per folder   5
    Protect shared    on
  Run
    Clear & prune  -  3 session(s)  (~222.4 MB)  +  VACUUM
    Vacuum only

Press Enter on a rule to cycle its value (or enter a custom number); the match count updates live. Then pick Clear & prune to confirm and reclaim.

Rules

Rule Values Meaning
Older than off / 1 day / 7 days / 30 days / 6 months / all sessions Delete sessions whose last activity is older than this.
Larger than off / 10 / 50 / 100 / 500 MB / 1 GB / custom Also delete sessions bigger than this.
Keep per folder 0 / 1 / 5 / 10 / 50 / 100 / custom Always keep at least this many of the newest sessions per folder, even if they match.
Protect shared on / off Never delete shared sessions.

A session is deleted when it is not protected (the currently open session, shared if protected, or among the newest N in its folder) and it matches the age rule or the size rule. "All sessions" matches everything not protected. Nothing is deleted until you pick Clear & prune and confirm.

Deletion goes through OpenCode's own session.delete API, so the running server tears down its state correctly; the plugin only does read-only planning and the final VACUUM.

Defaults

The panel's initial rule values come from env vars (set them where OpenCode is launched), so you can tune the starting point. A value of 0 means "off".

Variable Default Seeds
OPENCODE_VACUUM_OLDER_THAN_DAYS 30 "Older than" (in days; 0 = off)
OPENCODE_VACUUM_LARGER_THAN_MB 0 "Larger than" (in MB; 0 = off)
OPENCODE_VACUUM_KEEP_PER_FOLDER 5 "Keep per folder"
OPENCODE_VACUUM_PROTECT_SHARED true "Protect shared"
OPENCODE_DB (auto) Full path to opencode.db if it lives somewhere non-standard.

Install

Option 1: one-line installer

curl -fsSL https://raw.githubusercontent.com/anxkhn/opencode-vacuum/main/install.sh | bash

Option 2: manual

git clone https://github.com/anxkhn/opencode-vacuum
cd opencode-vacuum
cp vacuum.mjs        ~/.config/opencode/vacuum.mjs
cp vacuum-plugin.ts  ~/.config/opencode/vacuum-plugin.ts

Then declare the plugin in ~/.config/opencode/tui.json (required: TUI plugins are not auto-discovered from a directory). Create the file, or add the entry to your existing plugin array:

{
  "$schema": "https://opencode.ai/tui.json",
  "plugin": ["./vacuum-plugin.ts"]
}

Fully quit and reopen OpenCode so it loads the plugin.

Use it

In the OpenCode TUI, type:

/vacuum        # also available as /compactdb

Enter cycles a rule (custom rows open a numeric prompt); the match count updates live. Highlight Clear & prune and press Enter to confirm and reclaim, or Vacuum only to just defragment. Run it when OpenCode is idle (not mid-turn): VACUUM briefly takes a write lock on the database.

How it works

vacuum.mjs            engine: read-only session scan, applyRules, VACUUM
vacuum-plugin.ts      TUI plugin: the /vacuum rules panel, deletes, then vacuums
tui.json              declares the plugin so the TUI loads it
test/plugin.test.mjs  verifies annotation, rule matching, vacuum, and the panel

vacuum.mjs reads the session table (joined with per-session event byte sizes) read-only and exposes applyRules (a pure function) for matching; it runs PRAGMA wal_checkpoint + VACUUM to shrink the file. vacuum-plugin.ts renders the panel on api.ui.DialogSelect, deletes matching sessions via api.client.session.delete, then vacuums. It prefers bun:sqlite (OpenCode's runtime) and falls back to the built-in node:sqlite (Node 22+).

Test

node test/plugin.test.mjs

Uninstall

rm -f ~/.config/opencode/vacuum.mjs ~/.config/opencode/vacuum-plugin.ts

Then remove the "./vacuum-plugin.ts" entry from ~/.config/opencode/tui.json.

License

GPL-3.0

Contributors

anxkhn

Issues