nh-clean
removes old Nix profile generations and runs garbage collection
TLDR
SYNOPSIS
nh clean all|user [options]nh clean profile [options] profile
DESCRIPTION
nh clean is a more thorough replacement for nix-collect-garbage. It deletes old generations of Nix profiles according to count and age rules, removes orphaned gcroots and aged-out direnv gcroots, then runs nix store gc to free space in the Nix store.Unlike nix-collect-garbage, it can keep a minimum number of generations and a time window at the same time, and shows a plan of what it will delete before acting.
PARAMETERS
all
Clean all profiles: system profiles, per-user profiles and every user's XDG profiles. Usually needs root, which nh obtains itself.user
Clean the current user's profiles (~/.local/state/nix/profiles and /nix/var/nix/profiles/per-user/user). Refuses to run as root.profile path
Clean one specific profile. Gcroots are not touched in this mode.-k, --keep n
Keep at least this number of generations. Default: 1.-K, --keep-since duration
Keep gcroots and generations newer than this duration (humantime format, e.g. 3d, 2weeks, 12h). Default: 0h.-n, --dry
Only print what would be done, without doing it.-a, --ask
Ask for confirmation before proceeding.--no-gc
Don't run nix store gc after removing generations.--no-gcroots
Don't clean gcroots.--no-direnv
Don't clean gcroots created by direnv / nix-direnv.--keep-one
Keep at least one gcroot per direnv project.--optimise
Run nix-store --optimise after garbage collection.--max size
Pass --max to nix store gc, stopping after freeing this many bytes.-x, --cross-filesystems
Cross filesystem boundaries when scanning for gcroots.
INSTALL
CAVEATS
--keep takes a number of generations, not a duration; use --keep-since for time-based retention. Both rules are applied together: a generation is kept if either rule keeps it. The currently active generation is never removed.On NixOS, programs.nh.clean.enable can run nh clean periodically; do not enable it together with nix.gc.automatic.
SEE ALSO
nh(1), nix-collect-garbage(1), nix-store(1), direnv(1)
