1# Cleaning the Nix Store {#sec-nix-gc}
2
3Nix has a purely functional model, meaning that packages are never
4upgraded in place. Instead new versions of packages end up in a
5different location in the Nix store (`/nix/store`). You should
6periodically run Nix's *garbage collector* to remove old, unreferenced
7packages. This is easy:
8
9```ShellSession
10$ nix-collect-garbage
11```
12
13Alternatively, you can use a systemd unit that does the same in the
14background:
15
16```ShellSession
17# systemctl start nix-gc.service
18```
19
20You can tell NixOS in `configuration.nix` to run this unit automatically
21at certain points in time, for instance, every night at 03:15:
22
23```nix
24{
25 nix.gc.automatic = true;
26 nix.gc.dates = "03:15";
27}
28```
29
30The commands above do not remove garbage collector roots, such as old
31system configurations. Thus they do not remove the ability to roll back
32to previous configurations. The following command deletes old roots,
33removing the ability to roll back to them:
34
35```ShellSession
36$ nix-collect-garbage -d
37```
38
39You can also do this for specific profiles, e.g.
40
41```ShellSession
42$ nix-env -p /nix/var/nix/profiles/per-user/eelco/profile --delete-generations old
43```
44
45Note that NixOS system configurations are stored in the profile
46`/nix/var/nix/profiles/system`.
47
48Another way to reclaim disk space (often as much as 40% of the size of
49the Nix store) is to run Nix's store optimiser, which seeks out
50identical files in the store and replaces them with hard links to a
51single copy.
52
53```ShellSession
54$ nix-store --optimise
55```
56
57Since this command needs to read the entire Nix store, it can take quite
58a while to finish.
59
60## NixOS Boot Entries {#sect-nixos-gc-boot-entries}
61
62If your `/boot` partition runs out of space, after clearing old profiles
63you must rebuild your system with `nixos-rebuild boot` or `nixos-rebuild
64switch` to update the `/boot` partition and clear space.