# Nix-sweep: Clean up old Nix profile generations and left-over garbage collection roots

**URL:** https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428
**Category:** Links
**Created:** [July 7, 2025, 8:43am UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428 "2025-07-07T08:43:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jzbor](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jzbor/32/9281_2.png) [@jzbor](https://discourse.nixos.org/u/jzbor)
#### Post date: [July 7, 2025, 8:43am UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/1 "2025-07-07T08:43:53Z")

</div>

> **[GitHub - jzbor/nix-sweep](https://github.com/jzbor/nix-sweep)**
>
> Contribute to jzbor/nix-sweep development by creating an account on GitHub.

Hi everyone,

One of my greatest annoyances with NixOS the last few years was how untransparent store usage is. There were a few questions that seemed hard to answer easily with `nix` itself/standard Nix tooling. How much space does one of my system generations take? What generations do I have? How old are they? What gc roots do I have lingering around from things like `nix build .`? It was possible to answer some of these questions with a little bit of tooling and `nix-tree` was of great help as well, but what I really wanted is a cli utility to cover all these problems without much scripting required.

I was also annoyed with the gc mechanisms Nix brings out of the box. All you can specify is `--delete-older-than`. No possibility to specify “delete older 7 days, but keep at least 3 generations” or “delete all but the 5 newest generations”.

[nix-sweep](https://github.com/jzbor/nix-sweep) aims to help with both of these shortcomings. For example you can interactively delete user and home-manager generations older than 7 days, but keep at least 3 generations each:

```auto
$ nix-sweep cleanout --remove-older 7 --keep-min 3 user home

```

Or you can list all your lingering gc roots (including their age and size):

```auto
$ nix-sweep gc-roots

```

To get a summary for your system you can use:

```auto
$ nix-sweep analyze

```

This has improved my NixOS experience quite a bit: I can now use `nix build` without having to worry about gc roots wasting gigabytes of store space. I can remove generations based on their age on one day and based on a keep-max value on the next day. I can schedule a systemd service that removes old generations daily, while performing true garbage collections when necessary.

I hope this little program can be of help to some of you as well. I think especially newer and non-frequent users could benefit from a utility like this. If you have any feedback feel free to open an issue.

---

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mightyiam/32/33998_2.png) [@mightyiam](https://discourse.nixos.org/u/mightyiam)
#### Post date: [July 16, 2025, 5:47am UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/2 "2025-07-16T05:47:10Z")

</div>

Thanks. Gave it a try. Seems that it is aiming to be more comprehensive than `nh` with regards to analysis and cleanup. So I turned release notifications on. Keep it up.

---

<div class="post-metadata">

### Author: ![dragon\_logic](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/d/57b2e6/32.png) [@dragon\_logic](https://discourse.nixos.org/u/dragon_logic)
#### Post date: [August 5, 2025, 1:26pm UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/3 "2025-08-05T13:26:40Z")

</div>

Here’s what I currently have in my flake for garbage collection. How do I replace this with nix-sweep?

```bash

  nix.gc = {
    automatic = true;
    dates = "daily";
    options = "--delete-older-than 7d";
  };

```

---

<div class="post-metadata">

### Author: ![jzbor](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jzbor/32/9281_2.png) [@jzbor](https://discourse.nixos.org/u/jzbor)
#### Post date: [August 5, 2025, 6:35pm UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/4 "2025-08-05T18:35:07Z")

</div>

I would suggest you first try it out manually (`nix shell github:jzbor/nix-sweep` to get it into your environment) . You can get a quick overview of your Nix store usage with `nix-sweep analyze` and try out playing with the options `nix-sweep cleanout --help`.

If you want to replace your `nix.gc` option you can use the `nixosModule` in the flake, but note that it should be considered “experimental” right now and its interface may change in the future.

These options would give you the exact same behavior as above (provided you imported nix-sweep into your flake):

```nix
imports = [
  inputs.nix-sweep.nixosModules.default
];

services.nix-sweep = {
  enable = true;
  interval = "daily";
  removeOlder = "7d";
  gc = true;
  gcInterval = "daily";
};

```

`nix-sweep` has quite some more options than `nix.gc` and only really makes sense if you want to use these options. What settings you use/want to use depends a lot on your personal preference. Also atm it is only packaged for Flakes, so if you manage your system differently you will have to figure out how.

---

<div class="post-metadata">

### Author: ![dragon\_logic](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/d/57b2e6/32.png) [@dragon\_logic](https://discourse.nixos.org/u/dragon_logic)
#### Post date: [August 5, 2025, 8:02pm UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/5 "2025-08-05T20:02:08Z")

</div>

Thank you! I want to use this setting:

> [@jzbor](#):
>
> `--remove-older 7 --keep-min`

Basically I want to combine remove-older + keep-min. I’ll definitely test it out before putting it into my flake!

> If you want to replace your nix.gc option you can use the nixosModule in the flake, but note that it should be considered “experimental” right now and its interface may change in the future.

Can I do something like: `inputs.nix-sweep.url = "github:jzbor/nix-sweep";`

---

<div class="post-metadata">

### Author: ![jzbor](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jzbor/32/9281_2.png) [@jzbor](https://discourse.nixos.org/u/jzbor)
#### Post date: [August 6, 2025, 1:11pm UTC](https://discourse.nixos.org/t/nix-sweep-clean-up-old-nix-profile-generations-and-left-over-garbage-collection-roots/66428/6 "2025-08-06T13:11:47Z")

</div>

> [@dragon\_logic](#):
>
> Basically I want to combine remove-older + keep-min. I’ll definitely test it out before putting it into my flake!

This would probably look something like:

```Nix
services.nix-sweep = {
  enable = true;
  interval = "daily";
  removeOlder = "7d";
  keepMin = 10;
  gc = true;
  gcInterval = "daily";
};

```

Note that you can also just keep the regular Nix gc service instead of using the nix-sweep one:

```Nix
services.nix-sweep = {
  enable = true;
  interval = "daily";
  removeOlder = "7d";
  keepMin = 10;
};

nix.gc = {
  automatic = true;
  dates = "daily";
};

```

> [@dragon\_logic](#):
>
> Can I do something like: `inputs.nix-sweep.url = "github:jzbor/nix-sweep";`

Yes that should work. You might also have to add/update the `specialArgs` field for your configuration. You can refer to [Flakes - NixOS Wiki](https://nixos.wiki/wiki/flakes#Using_nix_flakes_with_NixOS) for detailed instructions.
