Version Retention, Pruning, and Table Metadata

# Use development build when interactive *and* explicitly enabled via env var.
dev_mode <- (Sys.getenv("DEV_VIGNETTES", "false") == "true")

if (dev_mode && requireNamespace("pkgload", quietly = TRUE)) {
  pkgload::load_all(
    export_all = FALSE,
    helpers = FALSE,
    attach_testthat = FALSE
  )
} else {
  # fall back to the installed package (the path CRAN, CI, and pkgdown take)
  library(stamp)
}
old_opts <- st_opts_get()
old_alias <- st_alias_get()

This vignette shows how to keep your versions store lean and your tables self-describing:

Key APIs:

Policy syntax (for retain_versions or st_prune_versions(policy=...)):

Implementation note: we recommend your package initializes retain_versions = Inf via st_opts() defaults. If you wire .st_apply_retention() at the end of st_save(), retention will be enforced automatically after each write. Otherwise, use st_prune_versions() explicitly.


Minimal project scaffold (temp)

root <- tempfile("stamp-retention-")
st_init(root)
## ℹ Rebasing default alias to new folder.
##   Existing:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-hashing-94044510696d/.stamp
##   Requested:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/.stamp
## ✔ stamp initialized
##   alias: default
##   root:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e
##   state:
##   /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/.stamp

We’ll create a few artifacts and multiple versions to demonstrate pruning:

pA <- "A.qs"
pB <- "B.qs"
pC <- "C.qs"

xA1 <- data.frame(a = 1:3)
xA2 <- data.frame(a = 2:4)
xA3 <- data.frame(a = 3:5)

xB1 <- data.frame(b = letters[1:3])
xB2 <- data.frame(b = letters[2:4])

xC1 <- data.frame(c = 10:12)

# Keep retention OFF initially so we can create multiple versions
st_opts(retain_versions = Inf)
## ✔ stamp options updated
##   retain_versions = "Inf"
st_save(xA1, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/A.qs'
##   @ version 290c63b79e4ae098
st_save(xA2, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/A.qs'
##   @ version 872c9e4657d30c5d
st_save(xA3, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/A.qs'
##   @ version 0ce66b0c3286f611
st_save(xB1, pB, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/B.qs'
##   @ version 9f6cf1dfe890e445
st_save(xB2, pB, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/B.qs'
##   @ version 610fb0f111a21a68
st_save(xC1, pC, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/C.qs'
##   @ version 0e1e4f4a08892504

Inspect store & catalog:

# Show version directories for each artifact (now stored per-artifact)
# Versions are stored in <artifact_folder>/versions/<version_id>/
info_a <- st_info(pA, alias = NULL)
artifact_dir_a <- fs::path_dir(info_a$sidecar$path)
versions_dir_a <- fs::path(artifact_dir_a, "versions")

if (fs::dir_exists(versions_dir_a)) {
  cat("A.qs versions:\n")
  fs::dir_tree(versions_dir_a, recurse = 1)
}

print(st_versions(pA, alias = NULL))
##          version_id      artifact_id     content_hash        code_hash
##              <char>           <char>           <char>           <char>
## 1: 0ce66b0c3286f611 73b433835100012d c9013165cc863a51 488e8fa49c740261
## 2: 872c9e4657d30c5d 73b433835100012d 84993efe38ccb7b3 488e8fa49c740261
## 3: 290c63b79e4ae098 73b433835100012d 62ba8debaab06c1c 488e8fa49c740261
##    size_bytes                  created_at sidecar_format
##         <num>                      <char>         <char>
## 1:        244 2026-11-12T16:27:32.830847Z           json
## 2:        244 2026-11-12T16:27:32.814810Z           json
## 3:        245 2026-11-12T16:27:32.798739Z           json
print(st_versions(pB, alias = NULL))
##          version_id      artifact_id     content_hash        code_hash
##              <char>           <char>           <char>           <char>
## 1: 610fb0f111a21a68 443090314fd0ac0d e36478ccde810d51 488e8fa49c740261
## 2: 9f6cf1dfe890e445 443090314fd0ac0d c215a395b52ae175 488e8fa49c740261
##    size_bytes                  created_at sidecar_format
##         <num>                      <char>         <char>
## 1:        200 2026-11-12T16:27:32.859673Z           json
## 2:        203 2026-11-12T16:27:32.845046Z           json
print(st_versions(pC, alias = NULL))
##          version_id      artifact_id     content_hash        code_hash
##              <char>           <char>           <char>           <char>
## 1: 0e1e4f4a08892504 f1be02c65bd154b4 497f4b4163bba213 488e8fa49c740261
##    size_bytes                  created_at sidecar_format
##         <num>                      <char>         <char>
## 1:        245 2026-11-12T16:27:32.874798Z           json

Ad-hoc pruning (explicit runs)

Use this when you want full control (e.g., pre-release cleanup, occasional housekeeping, or when you don’t wire auto-retention into st_save()).

Keep the n latest for a specific artifact

# Dry run (safe preview)
st_prune_versions(path = pA, policy = 2, dry_run = TRUE, alias = NULL)
## ✔ DRY RUN: 1 version would be pruned across 1 artifact.
##   Estimated space reclaimed: ~245 bytes
# Apply pruning
repA <- st_prune_versions(path = pA, policy = 2, dry_run = FALSE, alias = NULL)
repA
##         artifact_id
##              <char>
## 1: 73b433835100012d
##                                                                                   artifact_path
##                                                                                          <char>
## 1: /var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/A.qs
##          version_id                  created_at size_bytes protected reason
##              <char>                      <char>      <num>    <lgcl> <char>
## 1: 290c63b79e4ae098 2026-11-12T16:27:32.798739Z        245     FALSE policy
nrow(st_versions(pA, alias = NULL)) # <= 2; latest always protected
## [1] 2

Practical tip: always run the dry_run = TRUE preview and inspect repA before calling with dry_run = FALSE. The returned table indicates which snapshots would be removed and allows you to store that plan in CI logs for audit.

Keep by recency window across the entire catalog

# Keep anything from the last 14 days; preview first
st_prune_versions(policy = list(days = 14), dry_run = TRUE, alias = NULL)
## ✔ Retention policy matched zero versions; nothing to prune.
# Apply
repAll <- st_prune_versions(policy = list(days = 14), alias = NULL)
## ✔ Retention policy matched zero versions; nothing to prune.
head(repAll)
## Empty data.table (0 rows and 7 cols): artifact_id,artifact_path,version_id,created_at,size_bytes,protected...

Combine count + recency (union semantics)

# Keep last 2 versions OR any version created within 7 days
st_prune_versions(policy = list(n = 2, days = 7), alias = NULL)
## ✔ Retention policy matched zero versions; nothing to prune.

What the report returns

A data frame with artifact_path, version_id, created_at, and action (keep/delete). Use dry_run = TRUE to log/approve a plan before destructive actions (recommended on shared infra/CI).

Edge cases & guarantees


Automatic pruning (on every st_save())

If you call an internal .st_apply_retention() at the end of st_save(), you can choose a global policy and stop thinking about it:

# Keep only the latest 2 versions per artifact going forward
st_opts(retain_versions = 2)
## ✔ stamp options updated
##   retain_versions = "2"
# New save writes + immediate prune
xA4 <- data.frame(a = 4:6)
st_save(xA4, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] →
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/A.qs'
##   @ version fa63e507dab5ab0c
nrow(st_versions(pA, alias = NULL)) # <= 2
## [1] 2

If you enable automatic pruning via st_opts(retain_versions = <policy>), .st_apply_retention() will be invoked after each st_save() and prune according to the policy for the just-written artifact. This keeps your versions directory compact without extra housekeeping steps.

Be cautious when enabling aggressive retention (small n or short days) in shared or collaborative projects: set dry_run = TRUE in CI or have an approval step before destructive pruning.

Disable auto-pruning:

st_opts(retain_versions = Inf) # keep all versions
## ✔ stamp options updated
##   retain_versions = "Inf"

Where to set this For project-wide behavior, call st_opts(retain_versions = ...) in your project setup script after loading stamp.


Primary-key metadata & load-time checks

Milestone 4 adds PK metadata so each table carries its identity. This improves join safety, reproducibility, and downstream tooling (e.g., merge validation).

Inspect / repair PK later

st_inspect_pk(p_pop) # read PK from sidecar
## [1] "country"         "year"            "reporting_level"
# If an older artifact lacks PK or you need to repair metadata, use:
st_add_pk(p_pop, keys = c("country", "year", "reporting_level"))
## ✔ stamp options updated
##   require_pk_on_load = "FALSE"
## ✔ Loaded [qs2] ←
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/inputs/population.qs'
## ✔ Recorded primary key for 'inputs/population.qs' --> country, year,
##   reporting_level
## ✔ stamp options updated
##   require_pk_on_load = "FALSE"

Load-time behavior & options

obj <- st_load(p_pop, alias = NULL)
## ✔ Loaded [qs2] ←
##   '/var/folders/y8/8yc4jpc167gf18tx3w3b858m0000gp/T/RtmpwTUOez/stamp-retention-9404a7c258e/inputs/population.qs'
attr(obj, "stamp_pk") # keys attached on load
## $keys
## [1] "country"         "year"            "reporting_level"

Missing PK policy:

st_opts("require_pk_on_load", .get = TRUE) # default FALSE
## [1] FALSE
st_opts("warn_missing_pk_on_load", .get = TRUE) # default TRUE
## [1] TRUE
# CI: make PK presence a hard requirement
st_opts(require_pk_on_load = TRUE, warn_missing_pk_on_load = FALSE)
## ✔ stamp options updated
##   require_pk_on_load = "TRUE", warn_missing_pk_on_load = "FALSE"

Why PKs matter (joins & merges)

pop <- data.frame(
  country = c("PER", "MEX"),
  year = c(2023, 2022),
  pop = c(34.5, 126.7)
)
pop <- st_with_pk(pop, c("country", "year"))

gdp <- data.frame(
  country = c("PER", "MEX"),
  year = c(2023, 2022),
  gdp = c(0.27, 1.26)
)
gdp <- st_with_pk(gdp, c("country", "year"))

merged <- merge(pop, gdp, by = c("country", "year"))
attr(merged, "stamp_pk") <- list(keys = c("country", "year")) # preserve grain
merged
##   country year   pop  gdp
## 1     MEX 2022 126.7 1.26
## 2     PER 2023  34.5 0.27

Catalog corruption & safety

On rare occasions a catalog file can become unreadable (disk issues, manual edit, or process crash). The package is conservative:

Example recovery steps (manual):

  1. Move the corrupted catalog: mv <state>/catalog.qs2 <state>/catalog.qs2.bak
  2. Re-run st_save() on a representative artifact to recreate the catalog.

These steps are safe because pruning and catalog operations never touch the live artifact files — only the committed snapshots and the catalog.

Tips


Sidecar metadata (quick reference)

Every artifact has a JSON sidecar under sibling stmeta/ with:

Inspect:

side <- st_read_sidecar(p_pop)
names(side)
##  [1] "path"         "format"       "created_at"   "size_bytes"   "content_hash"
##  [6] "code_hash"    "file_hash"    "code_label"   "parents"      "attrs"       
## [11] "pk"
side$pk
## $keys
## [1] "country"         "year"            "reporting_level"

Integrity checks on load (if enabled via st_opts(verify_on_load = TRUE)):


Recommendations & Recipes


FAQ

Does pruning ever delete the latest version? No. The latest version per artifact is always protected.

What’s the union semantics of list(n, days)? A version is kept if it is among the n most recent or if its created_at falls within the days window.

Where are PKs stored? In each artifact’s sidecar JSON under sibling stmeta/. st_load() re-attaches them as attr(x, "stamp_pk").