Skip to content

feat: add --cleanup to remove local files - #52

Merged
raphaelcoeffic merged 1 commit into
mainfrom
feat/cleanup
Nov 28, 2026
Merged

raphaelcoeffic merged 1 commit into
mainfrom
feat/cleanup

Conversation

@raphaelcoeffic

Copy link
Copy Markdown
Owner

Implements the short-term roadmap item "Add a clean-up command to remove local files".

$ dive --cleanup
This will remove /home/user/.local/state/dive:
  base-img   716.00 MiB  base image, re-extracted on next run
  rootful    132.00 KiB  installed packages, shell history
  rootless    28.00 KiB  installed packages, shell history
Proceed? [y/N]

Behavior

  • Removes the whole state directory ($XDG_STATE_HOME/dive, or the build's CONFIG_STATE_DIR), plus the base image directory when --img-dir points outside of it.
  • Lists what will be removed with sizes, and asks for confirmation. --yes skips it, and is required when stdin is not a terminal.
  • Refuses to run while a dive session is open: a pid file in {rootful,rootless}/pids belonging to a live process named dive*. Stale pid files are ignored, as are reused PIDs of unrelated processes.
  • Makes the read-only Nix store directories writable before removing them (reuses chmod_dirs).
  • On permission errors (files owned by root, e.g. after running dive as root), points to sudo rm -rf <dir> — sudo dive --cleanup would resolve root's state directory instead.
  • --cleanup conflicts with a container ID or a command; --yes without --cleanup is a usage error. That check is explicit, as clap's requires counts a flag's default value as present.

The logic lives in a new dive::cleanup module (active_sessions, disk_usage, remove_all), with unit tests.

Testing

  • Unit tests: removal of a tree with read-only directories and a symlink (target untouched), live vs. stale vs. unrelated pid files.
  • Ran the binary against a fake state directory (XDG_STATE_HOME in a scratch dir): nothing to clean, live session (refused), no terminal without --yes (refused), answer n (aborted, nothing removed), answer y (removed), and the argument combinations.
  • Not run against a real state directory with root-owned files.

Docs

README: new "Removing local files" section under Usage; the roadmap item is removed.

dive keeps its base image, the packages installed with pkg and the
shell history in its state directory, with no way to remove them short
of finding the directory and making the read-only Nix store writable.

`dive --cleanup` lists what it will remove with sizes, asks for
confirmation (--yes skips it, and is required without a terminal), then
removes the state directory and a base image directory given outside of
it. It refuses to run while a dive session is open, and points to
`sudo rm -rf` when files are owned by root.

Removes the item from the README roadmap.
@raphaelcoeffic
raphaelcoeffic merged commit 3af3415 into main Nov 28, 2026
2 checks passed
@raphaelcoeffic
raphaelcoeffic deleted the feat/cleanup branch November 28, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant