Remove old or excess files from a cache directory.
manageCache(
cacheDir = NULL,
extensions = c("html", "json", "pdf", "png"),
maxCacheSize = 100,
sortBy = "atime",
maxFileAge = NULL
)Location of cache directory.
Vector of file extensions eligible for removal.
Maximum cache size in megabytes.
Timestamp used to order files for size-based removal. One of
"atime", "ctime", or "mtime".
Maximum file age in days. Files with modification times older than this value are removed regardless of cache size. Fractional days are allowed.
Invisibly returns the number of files removed.
Files are eligible for removal when their extension matches extensions.
Matching is case-sensitive and extensions may be supplied with or without a
leading dot.
Files can be removed for two reasons:
files older than maxFileAge days are removed first
if the remaining cache exceeds maxCacheSize, additional files are
removed until the cache is under the requested size
When removing files to satisfy maxCacheSize, files are ordered by the
timestamp specified by sortBy.
Timestamp meanings are:
atimeFile access time, updated when a file is opened.
ctimeFile change time, updated when file metadata changes.
mtimeFile modification time, updated when file contents change.
CACHE_DIR <- tempdir()
write.csv(matrix(1, 400, 500), file = file.path(CACHE_DIR, "m1.csv"))
write.csv(matrix(2, 400, 500), file = file.path(CACHE_DIR, "m2.csv"))
write.csv(matrix(3, 400, 500), file = file.path(CACHE_DIR, "m3.csv"))
write.csv(matrix(4, 400, 500), file = file.path(CACHE_DIR, "m4.csv"))
for (file in list.files(CACHE_DIR, pattern = "\\.csv$", full.names = TRUE)) {
print(file.info(file)[, c("size", "mtime")])
}
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m1.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m1.csv 2026-05-05 14:20:38
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m2.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m2.csv 2026-05-05 14:20:38
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m3.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m3.csv 2026-05-05 14:20:38
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m4.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m4.csv 2026-05-05 14:20:38
# Remove files based on access time until the cache is under 1 MB
manageCache(
CACHE_DIR,
extensions = "csv",
maxCacheSize = 1,
sortBy = "atime"
)
for (file in list.files(CACHE_DIR, pattern = "\\.csv$", full.names = TRUE)) {
print(file.info(file)[, c("size", "mtime")])
}
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m3.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m3.csv 2026-05-05 14:20:38
#> size
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m4.csv 405687
#> mtime
#> /var/folders/w_/wx_cnlqj3_v_hq0qrb8tmplr0000gn/T//RtmpTmvZgw/m4.csv 2026-05-05 14:20:38