How to remove old system generations?

How can I completely remove old nixos-generations?

I know that I can delete old generations with nix-collect-garbage or even nix-collect-garbage -d. But this seems not to work correctly for system generations.
So: How can I really list and remove old NixOS-system generations?


  • nix-collect-garbage -d
    was run (as root)
  • nix-env -p /nix/var/nix/profiles/system --list-generations
    lists exactly 1 configuration
  • /boot/grub/grub.cfg
    contains exactly 1 configuration (“NixOS - Default”)
  • but /nix/store contains e.g. several old linux-kernels, gcc etc.

Checking the reverse-dependencies of the old linux-kernels returns e.g.:

# nix-store --query --referrers /nix/store/0n0j02amv1vayml7qr7nlrl8rzrycgyf-linux-4.19.69

# nix-store --query --referrers /nix/store/832wnm2846j4avwicy1n7sar6yaijvnr-linux-4.19.69

# nix-store --query --referrers f38dx1144qc28hk1ji10f4klgmq4rp3f-linux-4.19.87

So, it seems that there are still some old nixos-system-generations, but they are not included in the list of generations and are not deleted from the garbage-collector.
How can I list and delete them?

1 Like

Strange. Run nix-store --query --roots /nix/store/... on any path to see what’s keeping it alive. They must be referred to by some root or else the GC would have worked.


There seem to be “result”-symlinks in strange places, linking to old generations.
Looks like they were left over from nixos-rebuild build-tests.

Didn’t expect that, so I guess NixOS - NixOS 21.05 manual should be improved to document this behavior there.

FWIW, it is explained in the nixos-rebuild man page :stuck_out_tongue: