Deleting tmp files


You can get the temp directory for the current R session. It does not change when called several times

tmp_dir <- tempdir()
tmp_dir
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh"
tempdir()
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh"

The temp directory contains the temp files and directory for the current R session

list.files(tmp_dir)
#> [1] "file16dc20539ab"  "file16dc4ad71f"   "file16dc5bab1716"
#> [4] "file16dc74d65663"

The session temp directory is in the temp directory of the system. You can use this path if you want to delete all in the temp directory of the system (not recommended though because it is for all the system, not just R temp files)

dirname(tmp_dir)
#> [1] "C:/Users/chris/AppData/Local/Temp"

This path is also contains in an environnement variable for the OS. (Obviously, I am on windows)

Sys.getenv("TEMP")
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp"
shell("echo %TMP%", intern = T) # command line from R on windows
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp"

tempfile() gives the path of a possible temporary file, in the tempdir() directory by default, with no file extension. The file is not created and tempfile gives different values when calls several times

tmp_file <- tempfile()
tmp_file
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh\\file16dc202636f"
file.exists(tmp_file)
#> [1] FALSE

tempfile() # new file path when called again
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh\\file16dc72594e58"

We can write something to tmp_file.

# file is created by writeLines if it does not exist (this is the case here)
writeLines("This is a temp file", con = tmp_file)
file.exists(tmp_file)
#> [1] TRUE

We can read from this file

readLines(tmp_file)
#> [1] "This is a temp file"

Now if you want to delete this file

file.remove(tmp_file)
#> [1] TRUE
file.exists(tmp_file)
#> [1] FALSE

If you want to delete all files in the R session temp folder, you can use file.remove on a list of files. For this example purpose, I deleted all temp file beginning with "file" ("^file" is a regex for that pattern). There are more than I created - R session seems to create some temp file along the way.

files <- list.files(tmp_dir, full.names = T, pattern = "^file")
files
#>  [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc1a6a6e15"
#>  [2] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc1ff572fc"
#>  [3] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc20539ab" 
#>  [4] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc2e2227b8"
#>  [5] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc4ad71f"  
#>  [6] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc513c35b6"
#>  [7] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc570a473f"
#>  [8] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc5bab1716"
#>  [9] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc6e102bd4"
#> [10] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc6f253f90"
#> [11] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/file16dc74d65663"
file.remove(files)
#> Warning in file.remove(files): impossible d'effacer le fichier 'C:
#> \Users\chris\AppData\Local\Temp\RtmpmusYkh/file16dc1ff572fc', à cause de
#> 'Permission denied'
#>  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

I get a warning because there is a file I can't delete (probably in use by R right now)

If you want to remove a folder you can use unlink too

# create a new directory under tempdir
dir.create(dir1 <- file.path(tempdir(), "testdir"))
# create 2 file under this new directory
file.create(file1 <- tempfile(tmpdir = dir1))
#> [1] TRUE
file.create(file2 <- tempfile(tmpdir = dir1))
#> [1] TRUE
file1
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/testdir\\file16dc26b5cb7"
file2
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/testdir\\file16dc2b0816fe"
list.files(dir1, full.names = T)
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/testdir/file16dc26b5cb7" 
#> [2] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpmusYkh/testdir/file16dc2b0816fe"

# we can delete the all directory with `unlink`. It deletes also the directory
unlink(dir1, recursive = T)
dir.exists(dir1)
#> [1] FALSE

After spending hours to find out how to use cmd to delete old files and folders I ended up writing this R script which I will definitely automate to run every week:

#Windows Environmental variable %USERPROFILE%\AppData\Local\Temp
PCTempDir <- Sys.getenv("TEMP")

#detect and delete folders with pattern "Rtmp"
folders <- dir(PCTempDir, pattern = "Rtmp", full.names = TRUE)
unlink(folders, recursive = TRUE, force = TRUE, expand = TRUE)

This will delete Rtmp* folders with files left by executed R scripts as I have a lot of them on my computer

Of course such script could be also expanded to accommodate other folders


Here's a very simple way

unlink(paste0(normalizePath(tempdir()), "/", dir(tempdir())), recursive = TRUE)

To confirm it worked

dir(tempdir())
# character(0)