77

Is there any way to automatically delete all files or folders with few R command lines? I am aware of the unlink() or file.remove() functions, but for those you need to define a character vector with exactly all the names of the files you want to delete. I am looking more for something that lists all the files or folders within a specific path (e.g. 'C:/Temp') and then delete all files with a certain name (regardless of its extension).

Any help is very much appreciated!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Francesco
  • 819
  • 1
  • 7
  • 7

6 Answers6

84

For all files in a known path you can:

unlink("path/*")
Hahnemann
  • 4,378
  • 6
  • 40
  • 64
  • 13
    Why isn't this the highest voted solution? This works wonderfully. – Zediiiii Mar 24 '16 at 18:55
  • 5
    One thing to note: this method deletes everything for good; files cannot be recovered within the recycling bin. – Dale Kube Jun 30 '17 at 02:37
  • 2
    Best solution, but sometimes does not work. `recursive=TRUE` & `force=TRUE` helps. I am on linux. – Ufos Apr 17 '18 at 11:07
  • This refuses to delete a directory in my windows 10 even with the recursive=TRUE and force=TRUE arguments. The structure of the directory is folder/folder/files, and I am trying to delete the top folder with unlink(). – Dan Woodrich Nov 27 '18 at 04:45
  • Any alternative that moves files to recycle bin/trash? – duncanrager Jun 18 '19 at 14:35
84

Maybe you're just looking for a combination of file.remove and list.files? Maybe something like:

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

And I guess you can filter the list of files down to those whose names match a certain pattern using grep or grepl, no?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
joran
  • 169,992
  • 32
  • 429
  • 468
  • 4
    And possibly using the `pattern` argument to `list.files` (or `dir` which is a synonym). I think they produce a vector by default, so need to wrap a `list` call around them to work with `do.call`. – James Feb 15 '12 at 15:56
  • 1
    Guys,when I try the do.call like you said I get this error message: [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Warning messages: 1: In function (...) : cannot remove file 'Sim2003.dbf', reason 'No such file or directory' – Francesco Feb 15 '12 at 16:29
  • Which makes it seem like there is no file (there are no files) in the directory I specify...that is kinda weird since the files are there...and if I just use the list.files('C:/Temp') it gives me all the right ones...what am I doing wrong? – Francesco Feb 15 '12 at 16:31
  • 7
    Ok, I solved it...you need to add full.names=TRUE inside the list.files() function...otherwise it does not find them just with relative path... – Francesco Feb 15 '12 at 16:36
  • 1
    You don't need `do.call`; see my answer. – Richie Cotton Feb 15 '12 at 17:20
29
dir_to_clean <- tempdir() #or wherever

#create some junk to test it with
file.create(file.path(
  dir_to_clean, 
  paste("test", 1:5, "txt", sep = ".")
))

#Now remove them (no need for messing about with do.call)
file.remove(dir(  
  dir_to_clean, 
  pattern = "^test\\.[0-9]\\.txt$", 
  full.names = TRUE
))

You can also use unlink as an alternative to file.remove.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • What if I want to delete files for which their position indice doesn't show in a position indice vector? For example, the files are:a.txt, b.txt, c.txt and my vector v <- c(1, 3). The result should only show files:a.txt and c.txt – Nanami Jul 23 '13 at 11:51
  • @Nanami: Read http://www.regular-expressions.info/quickstart.html and `?regex`. If you are still stuck, then ask a question with the tag `regex`. – Richie Cotton Jul 25 '13 at 09:38
  • Thanks. I used this in my code though I ended up deleting the `^` and the `$` in my regex and I think this is the change that made it work. Can't say that I tested this thoroughly. – Leonid May 29 '17 at 21:15
  • This is a great solution. Also works by just specifying the directory (for the edification of other less savvy R users like myself). "file.remove(dir(path = "01 Data/", pattern = "my_pattern", full.names = TRUE)) – glenn_in_boston Sep 27 '21 at 17:12
5

To delete everything inside the folder, but keep the folder empty

unlink("path/*", recursive = T, force = T)

To delete everything inside the folder, and also delete the folder

unlink("path", recursive = T, force = T)

Use force = T, to overwrite any read-only/hidden/etc. issues.

ishonest
  • 433
  • 4
  • 8
4

Using a combination of dir and grep this isn't too bad. This could probably be turned into a function that also tells you which files are to be deleted and gives you a chance to abort if it's not what you expected.

# Which directory?
mydir <- "C:/Test"
# What phrase do you want contained in
# the files to be deleted?
deletephrase <- "deleteme"

# Look at directory
dir(mydir)
# Figure out which files should be deleted
id <- grep(deletephrase, dir(mydir))
# Get the full path of the files to be deleted
todelete <- dir(mydir, full.names = TRUE)[id]
# BALEETED
unlink(todelete)
Dason
  • 60,663
  • 9
  • 131
  • 148
  • `dir` takes a `pattern` argument, so you don't need the separate use of `grep`. – Richie Cotton Feb 15 '12 at 17:09
  • @RichieCotton Good point. I'll take note of that as I don't really use dir all that often and grep seemed like a natural solution to me. – Dason Feb 15 '12 at 19:23
0

I quite like here::here for finding my way through folders (especially if I'm switching between inline evaluation and knit versions of an Rmarkdown notebook)... yet another solution:

    # Batch remove files
    # Match files in chosen directory with specified regex
    files <- dir(here::here("your_folder"), "your_pattern") 

    # Remove matched files
    unlink(paste0(here::here("your_folder"), files))
seapen
  • 345
  • 1
  • 4
  • 13