Deleting temporary files created by cmdstanr

I am running cmdstanr within an mclapply loop, e.g.

                                                   
mod <- cmdstan_model(stan_file='myModel.stan') 
results <-do.call(rbind,mclapply(1:1000,mc.cores=4,function(i) {  
  ...
  fit <- mod$sample(data=input_data,
                    chains=4,
                  parallel_chains=4)
  ...
})

Stan runs fine but it creates several large files within the rtmpfile /tmp/RtmpXXX and they do not get removed, but actually accumulate as the mclapply loop progresses and take up more and more space.

What can I do? Is there a way to figure out the temporary files that rcmdstan is creating so that I can delete them after each run? Any other suggestions?

The sample() function allows you to manually specify the name and location of the temp file where draws are written to. See Run Stan's MCMC algorithms — model-method-sample • cmdstanr

3 Likes

This works, one can get the output files from the fit and then delete them…

files_to_remove ← fit$output_files()
file.remove(files_to_remove)

1 Like