# Deleting temporary files created by cmdstanr

**URL:** <https://discourse.mc-stan.org/t/deleting-temporary-files-created-by-cmdstanr/32485>\
**Category:** General\
**Tags:** cmdstanr\
**Created:** [August 18, 2023, 7:54pm UTC](https://discourse.mc-stan.org/t/deleting-temporary-files-created-by-cmdstanr/32485 "2023-08-18T19:54:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramiro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ramiro/32/7587_2.png) [@ramiro](https://discourse.mc-stan.org/u/ramiro)\
**Post date:** [August 18, 2023, 7:54pm UTC](https://discourse.mc-stan.org/t/deleting-temporary-files-created-by-cmdstanr/32485/1 "2023-08-18T19:54:29Z")

</div>

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

```plaintext
                                                   
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?

---

<div class="post-metadata">

**Author:** ![LucC](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lucc/32/4475_2.png) [@LucC](https://discourse.mc-stan.org/u/LucC)\
**Post date:** [August 20, 2023, 7:18am UTC](https://discourse.mc-stan.org/t/deleting-temporary-files-created-by-cmdstanr/32485/2 "2023-08-20T07:18:33Z")

</div>

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](https://mc-stan.org/cmdstanr/reference/model-method-sample.html)

---

<div class="post-metadata">

**Author:** ![ramiro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ramiro/32/7587_2.png) [@ramiro](https://discourse.mc-stan.org/u/ramiro)\
**Post date:** [August 22, 2023, 3:42pm UTC](https://discourse.mc-stan.org/t/deleting-temporary-files-created-by-cmdstanr/32485/4 "2023-08-22T15:42:32Z")

</div>

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)
