Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to remove a directory in R?

  • Last Updated : 14 Sep, 2021

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!