# 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)
}
This vignette shows how to keep your versions store
lean and your tables self-describing:
- Retention & pruning: keep only the versions you
need, either automatically after each save or
on demand.
- Table metadata: record primary keys
(PKs) and sidecar metadata so downstream code understands the
grain and integrity of each table.
- Partitioned datasets: read/write tidy “Hive-style”
layouts (e.g.,
country=PER/year=2023/part.qs2) and bind
them efficiently.
Key APIs:
- Global retention:
st_opts(retain_versions = policy)
- Ad-hoc pruning:
st_prune_versions(path = NULL, policy = ..., dry_run = FALSE)
- PK metadata:
st_save(..., pk = c(...)),
st_add_pk(), st_inspect_pk()
- Partition helpers:
st_part_path(),
st_save_part(), st_list_parts(),
st_load_parts()
Policy syntax (for retain_versions or
st_prune_versions(policy=...)):
Inf (default): keep everything.
- Integer
n: keep the n latest versions
per artifact.
- List union:
list(n = 5, days = 14) keeps the 5
latest or anything from the last 14
days (latest is always protected).
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 <- fs::path(tempdir(), "stamp-retention-example")
if (fs::dir_exists(root)) {
fs::dir_delete(root)
}
st_init(root)
## ℹ Rebasing default alias to new folder.
## Existing: /tmp/Rtmp16hUjf/projA/.stamp
## Requested: /tmp/Rtmp16hUjf/stamp-retention-example/.stamp
## ✔ stamp initialized
## alias: default
## root: /tmp/Rtmp16hUjf/stamp-retention-example
## state: /tmp/Rtmp16hUjf/stamp-retention-example/.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] → '/tmp/Rtmp16hUjf/stamp-retention-example/A.qs' @ version
## c0822236862fe56d
st_save(xA2, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] → '/tmp/Rtmp16hUjf/stamp-retention-example/A.qs' @ version
## 627085c85c70a759
st_save(xA3, pA, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] → '/tmp/Rtmp16hUjf/stamp-retention-example/A.qs' @ version
## d33cbec1183ce93c
st_save(xB1, pB, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] → '/tmp/Rtmp16hUjf/stamp-retention-example/B.qs' @ version
## d61f4d3887820041
st_save(xB2, pB, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] → '/tmp/Rtmp16hUjf/stamp-retention-example/B.qs' @ version
## f56c89c2843c8257
st_save(xC1, pC, code = function(z) z, alias = NULL)
## ✔ Saved [qs2] → '/tmp/Rtmp16hUjf/stamp-retention-example/C.qs' @ version
## 5e096ddcf78182c8
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: d33cbec1183ce93c 9c3c79ac84c3b606 c9013165cc863a51 488e8fa49c740261
## 2: 627085c85c70a759 9c3c79ac84c3b606 84993efe38ccb7b3 488e8fa49c740261
## 3: c0822236862fe56d 9c3c79ac84c3b606 62ba8debaab06c1c 488e8fa49c740261
## size_bytes created_at sidecar_format
## <num> <char> <char>
## 1: 244 2026-12-05T15:46:48.120082Z json
## 2: 244 2026-12-05T15:46:48.089823Z json
## 3: 245 2026-12-05T15:46:48.055648Z json
print(st_versions(pB, alias = NULL))
## version_id artifact_id content_hash code_hash
## <char> <char> <char> <char>
## 1: f56c89c2843c8257 69afce16f85d4841 e36478ccde810d51 488e8fa49c740261
## 2: d61f4d3887820041 69afce16f85d4841 c215a395b52ae175 488e8fa49c740261
## size_bytes created_at sidecar_format
## <num> <char> <char>
## 1: 200 2026-12-05T15:46:48.184350Z json
## 2: 203 2026-12-05T15:46:48.154000Z json
print(st_versions(pC, alias = NULL))
## version_id artifact_id content_hash code_hash
## <char> <char> <char> <char>
## 1: 5e096ddcf78182c8 9c6043edae2af46e 497f4b4163bba213 488e8fa49c740261
## size_bytes created_at sidecar_format
## <num> <char> <char>
## 1: 245 2026-12-05T15:46:48.212862Z 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 artifact_path
## <char> <char>
## 1: 9c3c79ac84c3b606 /tmp/Rtmp16hUjf/stamp-retention-example/A.qs
## version_id created_at size_bytes
## <char> <char> <num>
## 1: c0822236862fe56d 2026-12-05T15:46:48.055648Z 245
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)
## ✔ DRY RUN: 1 version would be pruned across 1 artifact.
## Estimated space reclaimed: ~244 bytes
# Apply
repAll <- st_prune_versions(policy = list(days = 14), alias = NULL)
## ✔ DRY RUN: 1 version would be pruned across 1 artifact.
## Estimated space reclaimed: ~244 bytes
## artifact_id artifact_path
## <char> <char>
## 1: 9c3c79ac84c3b606 /tmp/Rtmp16hUjf/stamp-retention-example/A.qs
## version_id created_at size_bytes
## <char> <char> <num>
## 1: 627085c85c70a759 2026-12-05T15:46:48.089823Z 244
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
- Latest version is always protected (even if it
falls outside your policy window).
- Time windows are computed from each version’s
created_at in the catalog.
- Pruning is idempotent: re-running with the same
policy won’t remove more once the policy is satisfied.
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] → '/tmp/Rtmp16hUjf/stamp-retention-example/A.qs' @ version
## 8d04600deab5aaef
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, set
options(stamp.retain_versions = ...) in your project
.Rprofile. At runtime,
st_opts(retain_versions = ...) takes precedence.
Partitioned datasets (Hive-style)
When you need one file per key combo (e.g., per
country/year), use the partition helpers. Layout:
<base>/<k1>=<v1>/<k2>=<v2>/part.<ext>
Create partitions & save parts
base <- "inputs/country_year"
# Paths (order of keys doesn't matter; normalized internally)
p_per_2023 <- st_part_path(
base,
key = list(country = "PER", year = 2023),
format = "qs2"
)
p_mex_2022 <- st_part_path(base, key = list(country = "MEX", year = 2022))
per_tbl <- data.frame(country = "PER", year = 2023, pop = 34.5)
mex_tbl <- data.frame(country = "MEX", year = 2022, pop = 126.7)
# Save; PK recorded in each partition's sidecar
st_save_part(
per_tbl,
base,
key = list(country = "PER", year = 2023),
pk = c("country", "year"),
alias = NULL
)
## ✔ Saved [qs2] →
## '/tmp/Rtmp16hUjf/stamp-retention-example/inputs/country_year/country=PER/year=2023/part.qs2'
## @ version 731df6fa7cb019ab
st_save_part(
mex_tbl,
base,
key = list(country = "MEX", year = 2022),
pk = c("country", "year"),
alias = NULL
)
## ✔ Saved [qs2] →
## '/tmp/Rtmp16hUjf/stamp-retention-example/inputs/country_year/country=MEX/year=2022/part.qs2'
## @ version e5e84be05fb50fd0
st_save_part() uses st_save() under the
hood and writes sidecars in a local stmeta/ directory
inside each partition.
Discover & load
# List artifacts (sidecars under stmeta/ are ignored)
st_list_parts(base)
## [1] path
## <0 rows> (or 0-length row.names)
st_list_parts(base, filter = list(country = "PER"))
## [1] path
## <0 rows> (or 0-length row.names)
# Bind rows (adds key columns as ordinary columns)
all_parts <- st_load_parts(base, as = "rbind")
all_parts
## data frame with 0 columns and 0 rows
# data.table option (if installed)
if (requireNamespace("data.table", quietly = TRUE)) {
dt <- st_load_parts(base, as = "dt")
dt[]
}
## Null data.table (0 rows and 0 cols)
Notes
- If a partition artifact is not a
data.frame, st_load_parts() returns a one-row
table with a .object list-column and still appends the key
columns.
- Folder keys (e.g.,
country=PER/year=2023) should
agree with the partition table’s PK columns.
Recommendations & Recipes
- Safety first: always
dry_run = TRUE on
large catalogs; store the plan for auditability.
- Auto vs. manual: prefer auto
retention for day-to-day saves; add a manual
prune step in release or housekeeping jobs.
- Project defaults: set
options(stamp.retain_versions = ...) in
.Rprofile; override at runtime with
st_opts().
- CI: enforce
require_pk_on_load = TRUE
and run st_prune_versions(..., dry_run = TRUE) to produce a
log before destructive steps.
- Time windows: retention windows are based on
created_at recorded at version creation; keep machine
clocks sane on shared hosts.
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").
Do partition helpers change how retention works?
No. Each partition artifact is versioned/pruned independently,
inheriting the same retention policies.