Open In App

How to remove a directory in R?

Last Updated : 14 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to remove a directory using R programming language.

To remove a directory in R we use unlink(). This function deletes the named directory.

Syntax: unlink(directory-name, recursive = BOOLEAN)

Parameter:

  • directory-name: a character vector with the names of the directories to be deleted.
  • recursive: BOOLEAN TRUE/FALSE, if true directory is deleted recursively otherwise only empty directories are deleted.

Return: It returns normally 0 for success, 1 for failure. Not deleting a non-existent file is not a failure so in that case return 0.

Note: File naming conventions depend on the platform.

Directory in use:

Directory used

Example: Removing directory using R programming language

R




# list files or directories in working directory 
list.files(path=".", pattern=NULL, all.files=FALSE,
    full.names=FALSE)
  
# delete the directory demo in working directory
unlink("Demo",recursive=TRUE)
  
# list files or directories in working directory 
# after deletion
list.files(path=".", pattern=NULL, all.files=FALSE,
    full.names=FALSE)


Output:

Console Output

Final Directory after running the above code:

Final state of Directory


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads