diff --git a/doc/manual/command-ref/nix-env.xml b/doc/manual/command-ref/nix-env.xml
index f0e70a41c..7eb9cc855 100644
--- a/doc/manual/command-ref/nix-env.xml
+++ b/doc/manual/command-ref/nix-env.xml
@@ -1349,9 +1349,9 @@ special value old to delete all non-current
generations, a value such as 30d to delete all
generations older than the specified number of days (except for the
generation that was active at that point in time), or a value such as.
-+5 to delete all but the number of items specified.
-Periodically deleting old generations is important to make garbage
-collection effective.
++5 to only keep the specified items older than the
+current generation. Periodically deleting old generations is important
+to make garbage collection effective.
@@ -1461,7 +1461,7 @@ error: no generation older than the current (91) exists
Environment variables
-
+
NIX_PROFILE
Location of the Nix profile. Defaults to the
@@ -1475,6 +1475,6 @@ error: no generation older than the current (91) exists
-
+
diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc
index b43ec66f6..4c6af567a 100644
--- a/src/libstore/profiles.cc
+++ b/src/libstore/profiles.cc
@@ -160,19 +160,24 @@ void deleteGenerations(const Path & profile, const std::set & gens
void deleteGenerationsGreaterThan(const Path & profile, int max, bool dryRun)
{
PathLocks lock;
-
lockProfile(lock, profile);
int curGen;
+ bool fromCurGen = false;
Generations gens = findGenerations(profile, curGen);
-
for (auto i = gens.rbegin(); i != gens.rend(); ++i) {
- if (max) {
+ if (i->number == curGen) {
+ fromCurGen = true;
max--;
continue;
}
- if (i->number != curGen)
+ if (fromCurGen) {
+ if (max) {
+ max--;
+ continue;
+ }
deleteGeneration2(profile, i->number, dryRun);
+ }
}
}