unlink

0th

Percentile

Delete Files and Directories

unlink deletes the file(s) or directories specified by x.

Keywords
file
Usage
unlink(x, recursive = FALSE, force = FALSE)
Arguments
x

a character vector with the names of the file(s) or directories to be deleted. Wildcards (normally * and ?) are allowed.

recursive

logical. Should directories be deleted recursively?

force

logical. Should permissions be changed (if possible) to allow the file or directory to be removed?

Details

Tilde-expansion (see path.expand) is done on x.

If recursive = FALSE directories are not deleted, not even empty ones.

On most platforms ‘file’ includes symbolic links, fifos and sockets. unlink(x, recursive = TRUE) deletes the just symbolic link if the target of such a link is a directory.

Wildcard expansion is done by the internal code of Sys.glob. Wildcards never match a leading . in the filename, and files . and .. will never be considered for deletion. Wildcards will only be expanded if the system supports it. Most systems will support not only * and ? but also character classes such as [a-z] (see the man pages for the system call glob on your OS). The metacharacters * ? [ can occur in Unix filenames, and this makes it difficult to use unlink to delete such files (see file.remove), although escaping the metacharacters by backslashes usually works. If a metacharacter matches nothing it is considered as a literal character.

recursive = TRUE might not be supported on all platforms, when it will be ignored, with a warning: however there are no known current examples. Character classes such as [a-z] are supported. The metacharacter [ can occur in Windows filenames, and this makes it difficult to use unlink to delete such files (see file.remove). If a wildcard matches nothing it is considered as a literal character.

Windows cannot remove the current working directory, nor any file which is open nor any directory containing such a file.

UTF-8-encoded paths not valid in the current locale can be used.

Value

0 for success, 1 for failure, invisibly. Not deleting a non-existent file is not a failure, nor is being unable to delete a directory if recursive = FALSE. However, missing values in x are regarded as failures.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

file.remove.

Aliases
  • unlink
Documentation reproduced from package base, version 3.5.1, License: Part of R 3.5.1

Community examples

richie@datacamp.com at May 30, 2017 base v3.4.0

Basic usage involves passing a character vector of paths to files that you would like to delete. ```r dir_to_clean <- tempdir() # or wherever #create some files to test it with some_files <- file.path( dir_to_clean, paste("test", 1:5, "txt", sep = ".") ) file.create(some_files) # Check that these files exist dir(dir_to_clean, full.names = TRUE) # Remove them unlink(some_files) # Check that they no longer exist dir(dir_to_clean, full.names = TRUE) ``` You can also use `?` to match any single character, or `*` to match multiple wildcard characters. See [`glob2rx()`](https://www.rdocumentation.org/packages/utils/topics/glob2rx) for more info. This is especially useful for deleting all the files in a directory. ```r dir_to_clean <- tempdir() # or wherever #create some files to test it with some_files <- file.path( dir_to_clean, paste("test", 1:5, "txt", sep = ".") ) file.create(some_files) # Check that these files exist dir(dir_to_clean, full.names = TRUE) # Remove them unlink(file.path(dir_to_clean, "*")) # Check that they no longer exist dir(dir_to_clean, full.names = TRUE) ```