# Deleting conversations

Status: built 2026-11-24. Tier: core (`src/trash.c`: the record, restore and the purge, which keeps
the journal's invariants). Clock: durability. Record: one `pic_trash` row per deleted conversation
(schema 20). Default: on; purged 30 days after deleting.

## Why

There was no way to delete a conversation, at the terminal or from a phone. People delete the wrong
one and do not remember commands, so deleting is one key where conversations are listed, and undoing
it is as easy.

## The shape

- **Delete hides.** A `pic_trash(session_id, deleted_ms, purged_ms)` row hides a conversation from
  every list and from `latest` (`PIC_LISTED_SQL`); admission into it is refused in
  `choose_session`. Restoring deletes the row. No other table changes, so delete and restore are one
  small transaction each and need no lease.
- **Running is refused.** Deleting checks, in its transaction, that the conversation has no admitted
  request and that no other process holds its lease. A request admitted after the delete commits is
  refused, so nothing starts in it between the look and the row.
- **Purge empties, it does not remove.** `pic_check` counts on rows staying: entries are numbered
  without gaps, every finished tool call keeps one result, a delivered message's text equals its
  entry's, and other processes (`pic attach`, the phone) keep entry numbers as cursors, which a
  removed row would let SQLite hand out again. So a purge keeps every row and blanks the text: the
  prompt, entry bodies and provider content (an assistant entry keeps one empty stub per tool call),
  tool input and output, bash messages, hook context, verify output, error text, the file history's
  paths and the hashes that keep stored bytes, attachments, settled inbox commands, and the
  conversation's name, system prompt and tools. Usage and cost stay, so `pic usage` still adds up.
  Freed pages are overwritten (`secure_delete` for the purge); `pic prune` then drops image and file
  bytes nothing names.
- **Forks share entries.** A fork's path runs through the entries of the conversation it came from,
  so a purge empties only the requests of purged conversations that no other conversation's path
  (listed, or deleted and still restorable) reaches. The rest stay whole until that conversation is
  purged too; each later purge looks again.
- **Never under a running pic.** A purge takes each conversation's lease without waiting and skips
  one that is held or has an unfinished request; it runs with the hourly retention in `pic_open` (for
  those deleted 30 or more days ago) and from `pic sessions empty-trash` (all of them).

## The phone

`delete_session` and `restore_session` are pic's own commands in a `command` envelope (the
protocol note's amendments); `pic remote` runs them on the narrow open, and the next `sessions`
list leaves the conversation out or has it again.

## Not done

- Exports made before (`pic export`, `~/.pic/pic.sqlite`'s Pi lines, ctx's archive) keep their
  copies; the manual says so.
- Pi's `app.session.delete` binding (Ctrl+D) stays accepted with no view: Ctrl+D quits on an empty
  editor, and the picker's keys are `d`, Delete and Backspace.
