Repository navigation
[bug] opencode session delete allows deleting the active session, causing data loss #37975
Description
Description
opencode session delete <sessionID> accepts the currently-active session ID without warning or error. Deleting the active session removes its row from the SQLite database while opencode is still using it. On the next message, opencode fails with FOREIGN KEY constraint failed when trying to persist a step-completion message, permanently killing the session.
This is distinct from #21236 (TUI keybind deletion) — this is the CLI opencode session delete command.
Expected Behavior
opencode session delete should refuse to delete the currently-active session:
Error: Cannot delete the currently-active session. Close the session first.
Or require --force to proceed.
Actual Behavior
The session is deleted silently. The active opencode instance crashes on the next message:
FOREIGN KEY constraint failed
at Session.updateMessage
at SessionProcessor.cleanup
at SessionProcessor.process
at SessionPrompt.run
Impact
- Data loss: Session conversation, context, and in-progress work permanently lost
- Agent risk: AI agents performing batch cleanup can accidentally delete their own session
- No recovery: No undo or trash mechanism for
opencode session delete
Steps to reproduce
- Start an opencode session:
opencode - Note the session ID:
grep 'created id=' ~/.local/share/opencode/log/opencode.log | tail -1 - In a separate terminal:
opencode session delete <that-session-id> - Return to the opencode session and send a message
- Session errors with
FOREIGN KEY constraint failedand is unusable
Real-world occurrence
An agent session performing database cleanup:
- Classified 969 sessions into A (ephemeral), B (productive), C (low-activity)
- Built a filtered delete list (840 sessions, excluding productive)
- Generated a shell script via
opencode session delete - Script referenced wrong input file (all 969 instead of filtered 840)
- Script deleted the agent's own session mid-operation
- Agent crashed with
FOREIGN KEY constraint failed
The agent was Category B (productive, 844 events) and should not have been deleted. The bug is that opencode session delete didn't prevent self-deletion.
Suggested Fix
if (sessionID === currentSessionID) {
console.error("Error: Cannot delete the currently-active session.")
process.exit(1)
}Related Issues
- [bug / critical / data-loss] Accidental session deletion: Ctrl+Shift+Backspace deletes active session without confirmation #21236: Ctrl+Shift+Backspace deletes active session (TUI, not CLI) — closed
- [Bug] Session list focus jumps randomly when deleting via Ctrl+D in v1.1.25 (regression from #5466) #9236: Session list focus jumps when deleting (TUI)
- Session list jumps to active session when confirming delete on non-current session #5466: Session list jumps to active session on delete confirm (TUI)
- [FEATURE]: Subagent sessions accumulate without cleanup option - causes storage bloat and UI clutter #5734: Subagent sessions accumulate without cleanup option
Plugins
None
OpenCode version
v1.18.3
Operating System
Linux (Ubuntu, Hostkey VPS)
Terminal
CLI (non-TUI)
Activity
github-actions commentedon Sep 19, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.