LinuxCommandLibrary

nix-collect-garbage

Remove unused Nix store paths to free space

TLDR

Delete all store paths unused by current generations of each profile

$ sudo nix-collect-garbage [[-d|--delete-old]]
copy

Simulate the deletion of old store paths
$ sudo nix-collect-garbage [[-d|--delete-old]] --dry-run
copy

Delete all store paths older than 30 days
$ sudo nix-collect-garbage --delete-older-than 30d
copy

SYNOPSIS

nix-collect-garbage [options]

PARAMETERS

-d, --delete
    Actually delete garbage store paths. Without this option, the command only simulates the garbage collection process.

--dry-run
    Simulate the garbage collection process without deleting anything. This is the default behavior if `-d` is not specified.

--max-freed <bytes>
    Stop garbage collection after at least the specified number of bytes have been freed.

--min-age <age>
    Only collect store paths that have not been used in the specified age (e.g., '1d', '1w', '1m'). Defaults to 7 days.

-l, --list-dead
    List all store paths that would be deleted, even if they are not actually deleted.

-v, --verbose
    Show verbose output during the garbage collection process.

-f, --force
    Force garbage collection even if there are active builds or other processes that might be affected.

--print-live
    Print the store paths that are still considered live.

DESCRIPTION

nix-collect-garbage is a crucial command in the Nix package manager ecosystem for reclaiming disk space by removing unused store paths. The Nix store maintains all versions of packages and dependencies ever built or downloaded. Over time, this can lead to significant disk space consumption. nix-collect-garbage identifies and removes store paths that are no longer referenced by any Nix profile (the set of packages installed in a particular user's environment), active build processes, or other root paths explicitly kept alive.

The command offers options for controlling the garbage collection process, such as simulating a garbage collection run without actually deleting anything, specifying a minimum age for store paths to be considered garbage, and listing the paths that would be deleted. It's generally advisable to run nix-collect-garbage periodically to prevent the Nix store from growing excessively.

Important: Running garbage collection improperly *can* break your system, so it's recommended to be cautious and understand the impact before executing the command with deletion enabled. The -d option is very important.

CAVEATS

Incorrect use of nix-collect-garbage, especially with the -d and -f flags, can lead to system instability or broken packages. Exercise caution and understand the implications before running the command with deletion enabled. It is recommended to run a dry run first.

IMPORTANCE OF PROFILES

Nix profiles are a critical component. They are typically located in ~/.nix-profile (for user profiles) and /nix/var/nix/profiles/system (for the system profile). Make sure your profiles are sane and up to date before running garbage collection.

RUNNING AS ROOT

Garbage collection typically needs to be run as root to clean up the entire Nix store. Running it as a normal user will only clean up store paths that are owned by that user.

SEE ALSO

nix-store(1), nix(1)

Copied to clipboard