Deleting temporary files created by brms?

This is a related problem to Deleting temporary files created by cmdstanr

I’m trying to run a Monte Carlo simulation with brms and this creates a lot of temporary files in /tmp on linux due to left-over un-reusable Stan files. I’m wondering if there’s a way to automate deleting the files.

Using cmdstanr, this seems to be one way to get temporary files created

library(brms)
tmp<-brms::brm(
    count ~ zBase * Trt + (1|patient),
    data = epilepsy, family = poisson(),
    prior = prior(normal(0, 10), class = b) +
        prior(cauchy(0, 2), class = sd), backend = "cmdstanr"
)

tmp[["fit"]]@stan_args[[1]][["model"]]
tmp[["fit"]]@stan_args[[1]][["file"]]
tmp[["fit"]]@stan_args[[1]][["profile_file"]]

tmp[["fit"]]@stan_args[[1]][["model"]] needs to have _model removed from its end, but it’s workable.

Using rstan, I’m going to guess that these are the names of the temporary files in /tmp

library(brms)
tmp2<-brms::brm(
    count ~ zBase * Trt + (1|patient),
    data = epilepsy, family = poisson(),
    prior = prior(normal(0, 10), class = b) +
        prior(cauchy(0, 2), class = sd), backend = "rstan", stan_model_args = list(save_dso = FALSE)
)
tmp2[["fit"]]@stanmodel@dso@dso_filename

Are there any other temporary files I’m missing? What would be the easiest way to safely clean them up?

  • Operating System: Ubuntu 22.04
  • brms Version: `brms Version 2.22.6

Here is a snippet for as much as I could find regarding cmdstanr and brms. It assumes a list of brms models fitted using the cmdstanr backend. Chat GPT helped me with the regex

lapply(brms_als, \(x) {
   if (x$backend == "cmdstanr") {
     tmpdir <- x[["fit"]]@stan_args[[1]]$profile_file
     model_name <-x[["fit"]]@stan_args[[1]][["model"]]
     to_delete <- list.files(
       path = regmatches(tmpdir, regexpr("^.*(?=model)", tmpdir, perl = TRUE)), 
       pattern = paste0(regmatches(
         model_name, regexpr("model_.[^_]*", model_name)
       ), "*"),
       full.names = TRUE
     )
     file.remove(to_delete)
   } 
  })