Deleting tmp files

Doug Fir picture Doug Fir · Aug 26, 2017 · Viewed 13.3k times · Source

I read this post on how to delete tmp files. The solution in that post is:

do.call(file.remove, list(list.files("C:/Temp", full.names = TRUE)))

The directory "C:/Temp" is presumably fine with windows machines. Since I'm on linux I wasn't sure what my tmp file dir was so I did this:

tmp <- tempfile()

Which returns: "/tmp/RtmpNS80no/file147c37e54e8e"

So, I tried this:

do.call(file.remove, list(list.files("tmp", full.names = TRUE)))

This returns "logical(0)" so presumably did not work?

How do I delete all my tmp files?

Answer

cderv picture cderv · Aug 26, 2017


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