# How to remove old system generations?

**URL:** https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648
**Category:** Help
**Created:** [April 10, 2020, 4:01pm UTC](https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648 "2020-04-10T16:01:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rkoe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/rkoe/32/107_2.png) [@rkoe](https://discourse.nixos.org/u/rkoe)
#### Post date: [April 10, 2020, 4:01pm UTC](https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648/1 "2020-04-10T16:01:46Z")

</div>

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?

Details:

- `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.:

```auto
# nix-store --query --referrers /nix/store/0n0j02amv1vayml7qr7nlrl8rzrycgyf-linux-4.19.69
/nix/store/0n0j02amv1vayml7qr7nlrl8rzrycgyf-linux-4.19.63
/nix/store/ygcr4jynnr4cp1y0dxs1s8chx1x9k900-kernel-modules
/nix/store/aymqf7y6swzrnm0c467ghklcgk8b9ajz-nixos-system-MYHOST-19.03.173228.93ea6db29cc

# nix-store --query --referrers /nix/store/832wnm2846j4avwicy1n7sar6yaijvnr-linux-4.19.69
/nix/store/832wnm2846j4avwicy1n7sar6yaijvnr-linux-4.19.69
/nix/store/z81yd4pg3biy8zw8c675fa3qzycq1s0v-kernel-modules
/nix/store/100y8581l6qffgl9x8ligwm6k6731941-nixos-system-MYHOST-19.03.173408.bd6ba87381e

# nix-store --query --referrers f38dx1144qc28hk1ji10f4klgmq4rp3f-linux-4.19.87
/nix/store/f38dx1144qc28hk1ji10f4klgmq4rp3f-linux-4.19.87
/nix/store/yimd00zkkf2wpkybfcz7iax3xr0m9s7f-kernel-modules
/nix/store/4fw2cigw4680cq6jh1g4xj11905579pz-nixos-system-MYHOST-19.09.1529.808d3c6d123

```

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?

---

<div class="post-metadata">

### Author: ![ElvishJerricco](https://discourse.nixos.org/user_avatar/discourse.nixos.org/elvishjerricco/32/984_2.png) [@ElvishJerricco](https://discourse.nixos.org/u/ElvishJerricco)
#### Post date: [April 10, 2020, 11:41pm UTC](https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648/2 "2020-04-10T23:41:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![rkoe](https://discourse.nixos.org/user_avatar/discourse.nixos.org/rkoe/32/107_2.png) [@rkoe](https://discourse.nixos.org/u/rkoe)
#### Post date: [April 16, 2020, 11:20pm UTC](https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648/3 "2020-04-16T23:20:43Z")

</div>

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 23.11 manual | Nix & NixOS](https://nixos.org/nixos/manual/index.html#sec-changing-config) should be improved to document this behavior there.

---

<div class="post-metadata">

### Author: ![ElvishJerricco](https://discourse.nixos.org/user_avatar/discourse.nixos.org/elvishjerricco/32/984_2.png) [@ElvishJerricco](https://discourse.nixos.org/u/ElvishJerricco)
#### Post date: [April 17, 2020, 5:22am UTC](https://discourse.nixos.org/t/how-to-remove-old-system-generations/6648/4 "2020-04-17T05:22:16Z")

</div>

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