Session health

Deleted an OpenCode or Codex session? What can come back

Delete is permanent in OpenCode and Codex, archive is not. A 24-line backup script, a restore test, and what our app keeps. Versions and limits stated.

Kifah Abbad, smiling, in a scarf and jacket on a Berlin street

Kifah Abbad Published Last verified 12 min read

The short answer. A deleted OpenCode or Codex session cannot be restored by the tool: delete is permanent in both, and only archive can be undone. The one thing that brings a deleted session back is a copy you made earlier. A 24-line backup script and a tested restore are below.

The OpenCode database is copied with sqlite3 .backup; the newest dated copy sits in front of the older copies the script keeps.

Why this matters. A session is the record of hours of work: the decisions, the commands that were run, the reasons. If you delete one by mistake, or an update or a crash takes it, you cannot get it back unless a copy exists. This post shows which deletions can be undone, which cannot, and the small backup that makes the difference.

What you get. A 24-line backup script for OpenCode and Codex that is safe to run while the agents are open, a tested way to restore one session from it, and a clear list of what each tool can and cannot undo. You need it before the day you lose a session, not after.

I deleted a session with opencode session delete to see what happens. The session and all its messages were gone from the database, there was no trash, and nothing in the tool offered to undo it. Codex has the same kind of delete. Both tools do offer a safer button next to it, archive, and that one can be undone. Whether a deleted session comes back depends on whether you made a copy before.

Every claim below carries a label, because I have one Mac and cannot test everything: tested (run on 19 November 2026, OpenCode 2.3.0, on copies), estimated, assumed (follows from a smaller test or from the vendor’s source), or not tested.

Who needs this

Possible scenario. You spend an afternoon on a ticket with the agent. Next morning you clean up old sessions, or an update changes where the tool keeps them, and the one you needed is gone. Nothing in the tool offers an undo. A copy made the night before turns that into a two-minute restore; without it, the afternoon is gone.

RoleWhat changes for them
Junior software engineerCan tidy up sessions without fear, because archiving is safe and a backup exists for the rest.
DevOps or platform engineerGets a tested script to schedule and a clear list of what each tool can and cannot undo.
CTO or engineering leadLearns that delete is permanent and that nothing in the tools makes a backup, so it is a team practice or it does not exist.

Table: who gains what, my reasoning, not measured. I did not measure time saved for any role.

What happens when you delete

OpenCode 2.3.0, tested on a throwaway database. opencode session delete <id> removed the session row and its messages. I found no flag and no trash folder. The same call on a copy of my real database, 29 messages in the session, left 0 messages and one session fewer (1,211 of 1,212).

Codex, assumed from the source. I read the code on GitHub on 19 November 2026; the Codex command line is not installed here, so I ran nothing. codex delete is documented in the source as a local hard delete: it removes the session files, including compressed .jsonl.zst ones, and then the index rows. codex archive and codex unarchive only move a file between sessions/ and archived_sessions/. On my Mac one of 200 threads is archived and its file sits in archived_sessions/.

So delete is final in both tools, and archive is the undo-able one. Prefer archive when you only want a session out of the way.

What exists today to get a session back

RouteWhat it recoversStatus
OpenCode: unarchive with the API (PATCH /api/session/{id} with {"archived":false})An archived sessionTested. {"archived":null} did not work
OpenCode: session export before, session import afterA deleted session, same idTested: 29 of 29 messages and the same token counts
OpenCode: copy of the database made earlierEverything up to that momentTested on a copy, integrity check ok
Codex: codex unarchiveAn archived sessionAssumed from the source
Codex: copy of sessions/, archived_sessions/ and the index made earlierEverything up to that momentTested for one file: a deleted rollout copied back from the backup passed zstd -t
A community backup plugin for OpenCodeWhat its README says it copies: the storage/session, message and part foldersNot tested. On my Mac storage/ holds only plugin data and no session folder, so I assume it backs up nothing useful on 2.x

Table: what each route can recover, and how sure I am. Checked on 19 November 2026.

Two things from the community answers are out of date on 2.3.0. A GitHub question about recovering a deleted OpenCode session, closed in June, tells you to set time_archived to null in a table named session. Sessions now live in session_v2. I did not run those answers. And {"archived":null}, the obvious way to clear the flag, is silently ignored; only false restores. The vendor’s own field description says so.

None of these routes helps if you made no copy. That is the gap.

The gap: what a developer working with an agent would want

This is not available today. A session list with a trash that keeps deleted sessions for 30 days, an undo for delete, and a daily copy outside the data folder that the tool makes without being asked. My OpenCode data folder has a backup/ folder with three files from 9 August 2026. Their names end in .bak- and a timestamp, the same pattern as .bak- copies of config files that were made on this Mac by hand or by an agent, so I assume they were not made by OpenCode. The vendor’s troubleshooting page says to make a backup before inspecting the database and mentions no automatic one.

Ten minutes: a backup that survives a delete

Run this once by hand, then schedule it. It copies the OpenCode database with SQLite’s own backup command, which is safe while OpenCode is open (a plain cp of a live database can catch it half-written). It copies the Codex session folders and index, and never deletes anything it did not create.

#!/bin/sh
set -eu
DEST="${1:-$HOME/agent-session-backups}"
KEEP="${KEEP:-7}"
STAMP=$(date +%Y%m%d-%H%M%S)
CODEX_HOME="${CODEX_HOME:-$HOME/.codex}"
OC="$DEST/opencode/opencode-$STAMP.db"; TMP="$OC.partial"

P=$(dirname "$DEST"); [ -d "$P" ] || { echo "not found: $P" >&2; exit 1; }
mkdir -p "$DEST/opencode" "$DEST/codex"
trap 'rm -f "$TMP" "$TMP-journal" "$TMP-wal" "$TMP-shm"' EXIT

sqlite3 "$(opencode debug paths db)" ".backup '$TMP'"
sqlite3 "$TMP" "pragma integrity_check" | grep -qx ok
mv "$TMP" "$OC"
ls -1t "$DEST"/opencode/opencode-*.db | tail -n +$((KEEP+1)) \
  | while read -r old; do rm -f "$old"; done

rsync -a "$CODEX_HOME/sessions/" "$DEST/codex/sessions/"
rsync -a "$CODEX_HOME/archived_sessions/" "$DEST/codex/archived_sessions/"
sqlite3 "$CODEX_HOME/state_5.sqlite" \
  ".backup '$DEST/codex/state_5-$STAMP.sqlite'"
ls -1t "$DEST"/codex/state_5-*.sqlite | tail -n +$((KEEP+1)) \
  | while read -r old; do rm -f "$old"; done
echo "backed up to $DEST"
What the backup script does, in order A flowchart. The script checks that the folder above the destination exists; if not it stops with exit code 1 and writes nothing. It then copies the OpenCode database to a partial file and checks it. If the copy or the check fails it stops with exit code 1, removes the partial file and keeps the older copies. Otherwise it renames the copy and deletes all but the newest copies, copies the Codex files and index without deleting anything, and prints where it backed up to. Folder above the destination exists? No Stop, exit 1 tested Yes Copy the OpenCode database, check it Fail Stop, exit 1 old copies kept full disk: tested bad check: assumed Ok Keep the newest KEEP OpenCode copies Copy Codex files and index, never delete Print where it backed up to
What the script above does, in the order it runs. A missing destination and a full disk were tested; a failed integrity check is assumed to take the same exit as a full disk.

Save it as agent-backup.sh and run sh agent-backup.sh (it also ran under bash and zsh, see Windows and Linux). To restore one OpenCode session from a copy, point OPENCODE_DB at the copy and export, then import into the database you want it in:

OPENCODE_DB=~/agent-session-backups/opencode/opencode-STAMP.db \
  opencode --standalone session export ses_YOURID > one.json
OPENCODE_DB=/path/to/target.db \
  opencode --standalone session import one.json

I ran that flow on two copies of my database: delete in one, export from the other, import back. The 29 messages returned, the other copy was unchanged, and the test took 9 seconds. Not tested: importing into the database a running OpenCode is using. Quit OpenCode first, or use a copy and check it before you replace anything.

How long and how big. On my Mac, with a 1.8 GB OpenCode database and 81 MB of Codex files, a run takes 5 to 7 seconds and each OpenCode copy is 1.8 GB (tested). Three runs used 5.4 GB. Because the script keeps the newest 7 copies, I estimate it holds about 13 GB at the default KEEP=7 (7 × 1.8 GB = 12.6 GB, plus one copy while it runs), and that grows with your database. Set KEEP=3 if disk is tight. The Codex part adds little, as rsync copies only what changed (tested on a growing file).

What happens when it goes wrong.

  • Disk full. Tested on a 6 GB disk image I filled to the brim: the script stopped with an error, left no half-written copy, and the older copies stayed intact and passed integrity_check. I assume it behaves the same on a full external disk, and that it also needs room for one more copy while it runs, because it copies before it prunes. I did not test a real external disk.
  • Disk unplugged. Tested with a missing path: it stops with exit code 1 and writes nothing. A real unplug, mid-copy, was not tested.
  • Not scheduled. A script you run by hand protects you only on the days you run it. Scheduling it (launchd, cron, a systemd timer or Task Scheduler) is not tested here.

Off the machine. A copy on the same disk does not survive a failed disk or a stolen laptop. Point DEST at an external disk, or encrypt the copy before it goes to any cloud folder: the database holds your prompts, file contents and tool output, and nothing redacts a raw copy. gpg --symmetric (AES-256) encrypted a 50 MB slice of a real backup in 2 seconds and decrypted it in 1, identical to the original, and the readable text was gone from the encrypted file (tested). Estimate: if the cost is linear, the full 1.8 GB takes about 70 seconds to encrypt (2 s × 36, rounded). I did not upload anything to a cloud service, and I did not test a sync folder.

If you used our app: a readable copy, not a backup

AgentsOfMine is our product, and this section is about it. If you had synced a session with the collector before you deleted it, the app still holds a readable version, and you can read it or export it. Settings → Export my data gives a JSON file per project. To start, npm install -g agentsofmine, then aom pair; the collector docs list exactly what it reads and uploads.

What it is not: a backup. The collector uploads at most 150 messages per session (the first one and the latest 149) for OpenCode, Claude Code and Pi, and cuts tool output to 500 characters. The Codex reader applies no message cap. Common secret formats are replaced with a marker such as <REDACTED:github-token>, on a best-effort basis. It reads only files that exist when it runs, so a session you deleted before the first sync is not in the app, and you cannot import the app’s copy back into OpenCode or Codex.

I read the app’s backend source (a private repository, so you cannot check it) and the collector’s code (version 0.1.3, commit d5675de). The sync route only writes: it upserts sessions and messages with put_item calls. The only routes that delete anything are DELETE /devices/{id}, which switches a device off, and DELETE /account. Nothing deletes one session or message. So I assume a session you synced stays in the app after you delete it locally, and that deleting it locally changes nothing there. I did not test it end to end. To check it yourself: sync, delete a throwaway session, reload the app.

Windows and Linux

Tested on Linux and macOS; Windows documented or assumed, nothing run there. On 20 November 2026 I ran the script on macOS and in an Ubuntu 24.04 container (arm64), in a throwaway home with a fake opencode that only prints a database path. Labels: tested, documented (a vendor page read that day) and assumed (my reading).

I changed the script today. The 19 November version started with #!/bin/zsh and had two faults. With bash 3.2.57, the one that macOS ships as /bin/bash, it stopped at the line with ${DEST:h} (h: unbound variable) and backed up nothing. Under zsh it printed no matches found: ...partial* at the end, because of the trap pattern, though the backup was complete. The version above uses dirname and lists the temporary file’s leftovers by name, so it needs no zsh. The tested labels in this post for the backup, the missing folder and the full disk now rest on the new script, re-run today on throwaway data; the sizes and times under “How long and how big” were measured with the 19 November script on my real data and I did not repeat them.

  • Linux. The fixed script backed up and passed integrity_check under sh (dash), bash 5.2.21 and zsh 5.9; a missing destination stopped it with exit 1; KEEP=2 kept two copies after three runs; rsync -a kept a rollout I had deleted from the source (all tested). On macOS (bash 3.2.57, zsh 5.9, sh) the same checks passed, and so did the post’s two failure tests on the new script: an 8 MB disk image filled by the copy (exit 1, the older copy intact, no partial file) and the missing folder (tested). A bare Ubuntu 24.04 image has no sqlite3 or rsync; install both first (tested).
  • Windows, native. The script is sh. SQLite publishes a ready sqlite-tools-win-x64 (and arm64) zip containing the shell (documented: sqlite.org download page), and Microsoft’s robocopy is the nearest to rsync -a: /E copies subdirectories and does not delete in the destination, while /MIR adds /PURGE and does (documented: Microsoft Learn). I wrote no PowerShell version, and the pruning lines (ls -1t | tail) have no counterpart I checked.
  • Windows, WSL. Microsoft says to keep Linux project files in the Linux file system, not under /mnt/c (documented). I assume the script runs unchanged in WSL, but the sessions of a Windows-installed agent sit under %USERPROFILE%, which WSL reaches as /mnt/c/Users/<you>, so point the paths there (assumed). Where each agent keeps sessions: session formats.
  • Scheduling. Microsoft documents schtasks /create /sc DAILY, systemd documents timers with OnCalendar=, and POSIX has crontab (all documented; I ran none).

What this post does not cover

  • Claude Code. Its sessions are deleted after 30 days by default, which is a different problem; our docs page covers it.
  • Windows and Linux. Nothing ran on Windows. On Linux I ran the script, not OpenCode or Codex themselves; see Windows and Linux.
  • Restoring a session you never synced and never copied. I know of no way.
  • Versions. I ran OpenCode 2.3.0 (released 13 November). GitHub’s “latest release” label points at the older 1.x line (v1.19.6), which is why it looks lower. The 2.x line has v2.3.1 and v2.3.2, both released on 18 November. I searched their release notes for archive, delete, import, export, backup and restore and found nothing about sessions, so I assume the results hold on 2.3.2; I did not re-run them. Codex: my files come from several older versions, and the newest release on 18 November was rust-v0.170.0.
  • One machine. The sizes and times are mine.