From 5e8a88a05f71bdfd34a0a4a0854f190cf952606c Mon Sep 17 00:00:00 2001
From: Pascal Bleser
Date: Thu, 19 Nov 2026 00:02:20 +0100
Subject: [PATCH] feat: 1419: clean up revisions by the time they were created
Follows opencloud-eu/reva#895, which makes the revision cleanup go by
the time a revision was created, recorded by the server, instead of the
modification time of its content, which clients choose with X-OC-Mtime.
"auto" keeps the oldest revision of each slot (#3735).
- The storage-users README explains the new age of a revision.
- The acceptance scenario outline that checked the age as the time the
content was written is replaced by two scenarios: versions whose
content is dated 60 and 50 days ago are kept by "30d", and a version
with content dated 10 years ahead doesn't push out the newest one
under "1".
Assisted-by: Claude Code:claude-opus-5-5
---
services/storage-users/README.md | 4 +-
services/storage-users/pkg/config/config.go | 2 +-
.../cliCommands/cleanupFileVersions.feature | 38 ++++++++++++-------
3 files changed, 28 insertions(+), 16 deletions(-)
diff --git a/services/storage-users/README.md b/services/storage-users/README.md
index 3b20097953..fb583dad21 100644
--- a/services/storage-users/README.md
+++ b/services/storage-users/README.md
@@ -377,7 +377,7 @@ Durations in days or weeks can use an uppercase unit, `D` or `W`, to count from
A restore keeps the revision of the content it replaced as long as the file has the restored content: the strategy doesn't apply to that one revision, not even an age limit like `30d`, until the file is changed again, by an upload, another restore or, with `posix`, a change on disk.
-Note that the age of a revision is the time its content was written, i.e. the modification time the file had before it was changed, which is also the date the version list shows for it. It is not the time the file was changed, which turned that content into a revision. A document that was unchanged for two months gets a revision that is two months old when it is changed, and e.g. `30d` deletes that revision right away. Combine a duration with a number of revisions, e.g. `5, 30d`, to always keep the newest ones.
+Note that the age of a revision is the time it was created, i.e. when the file was changed and its previous content became the revision, by the clock of the server. The revisions are also ordered by it, e.g. for a number of revisions to keep. It is not the modification time of the content of the revision, which is the date the version list shows for it, as clients can set that to any time. A document that was unchanged for two months gets a revision that is a few seconds old when it is changed, and e.g. `30d` keeps that revision for 30 days. `auto` keeps the oldest revision of each time slot, which holds the content the file had when the slot began. Revisions created before OpenCloud recorded the time of their creation get an estimate: the time they were replaced, as far as it can be told from the dates of the newer revisions and of the file.
#### Selecting the Files
@@ -415,7 +415,7 @@ By default, the command prints a line for every file whose revisions it deleted,
1520 files in 3 spaces: deleted 3 revisions (4.2 MiB) of 1 file, 1 skipped, 0 failed
```
-With `--verbose`, every revision is listed with the decision (`deleted`, `kept` or `protected`), its time, its size and the reason:
+With `--verbose`, every revision is listed with the decision (`deleted`, `kept` or `protected`), its time, its size and the reason. The time is when the revision was created, see above, which can differ from the date the version list shows for it:
```plaintext
25b69780-5f39-43be-a7ac-a9b9e9fe4230 /dir1/file1 (node 43ca5408-c77b-4d9a-8a3b-d4470b1253b8): would delete 1 of 3 revisions (1.2 KiB)
diff --git a/services/storage-users/pkg/config/config.go b/services/storage-users/pkg/config/config.go
index 2e5012545e..bfeefbaa09 100644
--- a/services/storage-users/pkg/config/config.go
+++ b/services/storage-users/pkg/config/config.go
@@ -229,7 +229,7 @@ type Events struct {
// RevisionsCleanup configures the deletion of old revisions, for the 'decomposed', 'decomposeds3' and 'posix' storage drivers
type RevisionsCleanup struct {
- Strategy string `yaml:"strategy" env:"STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY" desc:"Which old revisions (versions) of a file to delete whenever the file gets a new revision. 'disabled' keeps all revisions. A strategy is a comma-separated list of up to three parts: a number of revisions, which counts from the newest one (e.g. '10'), a duration with one of the units 'w', 'd', 'h', 'm' or 's' (e.g. '30d'; with the uppercase units 'D' and 'W', days and weeks count from midnight, e.g. '1D'), and 'auto', which thins out revisions the older they get. 'X' deletes everything beyond X, e.g. '10', '30d', or '0' to delete all revisions. 'X, auto' keeps everything within X and thins out the older revisions, e.g. '14d, auto'. 'auto, X' deletes everything beyond X and thins out the rest, e.g. 'auto, 90d'. 'X1, X2' keeps everything within X1 and deletes everything beyond X2, e.g. '5, 30d'. 'X1, auto, X2' also thins out in between, e.g. '7d, auto, 90d'. The age of a revision is the time its content was written, i.e. the modification time the file had before it was changed, not the time of that change. Only files that get a new revision are cleaned up, including revisions created from changes on disk with posix. An invalid value makes the storage driver fail at startup." introductionVersion:"%%NEXT%%"`
+ Strategy string `yaml:"strategy" env:"STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY" desc:"Which old revisions (versions) of a file to delete whenever the file gets a new revision. 'disabled' keeps all revisions. A strategy is a comma-separated list of up to three parts: a number of revisions, which counts from the newest one (e.g. '10'), a duration with one of the units 'w', 'd', 'h', 'm' or 's' (e.g. '30d'; with the uppercase units 'D' and 'W', days and weeks count from midnight, e.g. '1D'), and 'auto', which thins out revisions the older they get. 'X' deletes everything beyond X, e.g. '10', '30d', or '0' to delete all revisions. 'X, auto' keeps everything within X and thins out the older revisions, e.g. '14d, auto'. 'auto, X' deletes everything beyond X and thins out the rest, e.g. 'auto, 90d'. 'X1, X2' keeps everything within X1 and deletes everything beyond X2, e.g. '5, 30d'. 'X1, auto, X2' also thins out in between, e.g. '7d, auto, 90d'. The age of a revision is the time it was created, i.e. when the file was changed and its previous content became the revision, not the modification time of that content, which the version list shows. Only files that get a new revision are cleaned up, including revisions created from changes on disk with posix. An invalid value makes the storage driver fail at startup." introductionVersion:"%%NEXT%%"`
Async bool `yaml:"async" env:"STORAGE_USERS_REVISIONS_CLEANUP_ASYNC" desc:"Run the revision cleanup in background workers, so that it does not slow down the upload, restore or change that created the new revision. When set to false, the cleanup runs right away." introductionVersion:"%%NEXT%%"`
AsyncWorkers int `yaml:"async_workers" env:"STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_WORKERS" desc:"Number of background workers for the revision cleanup. A value of 0 determines it automatically: half the available CPUs, respecting container CPU limits, but at least 2." introductionVersion:"%%NEXT%%"`
AsyncQueueSize int `yaml:"async_queue_size" env:"STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_SIZE" desc:"Number of files that can wait for a background revision cleanup. A value of 0 determines it automatically: 64 per worker." introductionVersion:"%%NEXT%%"`
diff --git a/tests/acceptance/features/cliCommands/cleanupFileVersions.feature b/tests/acceptance/features/cliCommands/cleanupFileVersions.feature
index e6802fdf6b..6c8a60da33 100644
--- a/tests/acceptance/features/cliCommands/cleanupFileVersions.feature
+++ b/tests/acceptance/features/cliCommands/cleanupFileVersions.feature
@@ -197,26 +197,38 @@ Feature: clean up old file versions according to a cleanup strategy
Then the number of versions of file "textfile.txt" for user "Alice" should eventually be "2"
And the content of file "textfile.txt" for user "Alice" should be "version 5"
- # The age of a version is the time its content was written, i.e. the modification time the
- # file had before it was changed, not the time of that change: "version 1" is 60 days old
- # as soon as it becomes a version.
- Scenario Outline: the configured strategy measures the age of a version by the time its content was written
+ # The age of a version is the time it was created, i.e. when its content was replaced, not
+ # the modification time of its content, which the client chooses: "version 1" and "version 2"
+ # are only seconds old as versions, although their content is dated 60 and 50 days ago.
+ Scenario: the configured strategy measures the age of a version by the time it was created
Given the following configs have been set:
- | config | value |
- | STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY | |
- | STORAGE_USERS_REVISIONS_CLEANUP_ASYNC | false |
+ | config | value |
+ | STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY | 30d |
+ | STORAGE_USERS_REVISIONS_CLEANUP_ASYNC | false |
And user "Alice" has uploaded a file with content "version 1" and mtime "-60 days" to "textfile.txt"
And user "Alice" has uploaded a file with content "version 2" and mtime "-50 days" to "textfile.txt"
And user "Alice" has uploaded file with content "version 3" to "textfile.txt"
When user "Alice" gets the number of versions of file "textfile.txt"
Then the HTTP status code should be "207"
- And the number of versions should be ""
+ And the number of versions should be "2"
+ And the content of file "textfile.txt" for user "Alice" should be "version 3"
+
+ # The versions are ordered by the time they were created as well: a version whose content
+ # is dated years ahead doesn't count as the newest one, so "1" keeps the version created
+ # last, "version 2", and not "version 1".
+ Scenario: a version with content dated in the future doesn't take the place of the newest one
+ Given the following configs have been set:
+ | config | value |
+ | STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY | 1 |
+ | STORAGE_USERS_REVISIONS_CLEANUP_ASYNC | false |
+ And user "Alice" has uploaded a file with content "version 1" and mtime "+10 years" to "textfile.txt"
+ And user "Alice" has uploaded file with content "version 2" to "textfile.txt"
+ And user "Alice" has uploaded file with content "version 3" to "textfile.txt"
+ When user "Alice" gets the number of versions of file "textfile.txt"
+ Then the HTTP status code should be "207"
+ And the number of versions should be "1"
+ And the content of version index "1" of file "textfile.txt" for user "Alice" should be "version 2"
And the content of file "textfile.txt" for user "Alice" should be "version 3"
- Examples:
- | strategy | versions |
- | 30d | 0 |
- | 1,30d | 1 |
- | 90d | 2 |
# A restore makes the content from before the restore a version, which the cleanup keeps
# whatever the strategy, so that the restore can be undone. The restored version itself