Skip to content
Related Articles

Related Articles

How to remove a directory in R?

View Discussion
Improve Article
Save Article
  • 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)


  • 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


# list files or directories in working directory 
list.files(path=".", pattern=NULL, all.files=FALSE,
# delete the directory demo in working directory
# list files or directories in working directory 
# after deletion
list.files(path=".", pattern=NULL, all.files=FALSE,


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!