How To Delete a Directory in R? (Using the Unlink Function)

Sometimes you need to remove a directory that is no longer needed from within your program, especially if you’re automatically generating packages of reports and data for other team mates. R programming provides a simple function for performing this task. You can even turn it on and off to prevent accidental deletion. This makes it possible for your program to remove a directory that is no longer needed.

How to Remove A Directory in R

The function that R programming provides for removing directories is the unlink function. This function has the format of unlink(“directory name”, recursive) where the directory name is the name of the directory to be deleted and recursive decides whether or not to actually delete the directory. If recursive is true then the directory gets deleted. If recursive is false, the directory does not get deleted. This provides an easy internal control for the function preventing accidental removal. As you can see the function is easy to use because it only has two arguments. The first argument is simply the directory name and the second is simply a boolean argument. This allows for both the selection of the directory and deciding whether or not to actually remove it.

How the Unlink function works in R

The unlink function takes in the name of the directory to be deleted and then looks at the recursive argument to see whether or not it is to actually remove it. This is both a protective measure to prevent accidental deletion, and it serves as a way of controlling when a directory gets deleted. When a directory is deleted so are any files within it. This is one reason you have to be very careful when removing directories, that is there is always a risk of deleting something you want to keep. This can be illustrated by moving a desktop folder with files in it to the recycle bin. You will see that the Files Go with it.

Examples of Removing Directories using Unlink

The following examples of R programming code shows how to delete a directory from the directory in which your R script file is located. When removing a directory from a different directory you simply need to include the entire path.

> unlink(“New folder”, recursive = TRUE)

In this example, we have a directory called “New folder” that is deleted by using the unlink function. Note that recursive is set to true and that the directory gets deleted.

> unlink(“test”, recursive = FALSE)

In this example, we have a directory called “test” to which the unlink function is applied. Note that recursive is set to false and that the directory does not get deleted.

> unlink(“test”, recursive = TRUE)

In this example, we have a directory called “test” that is deleted by using the unlink function. Note that recursive is set to true and that the directory gets deleted.

Application of the Unlink Function in R

The primary application of the unlink function would be the removal of temporary directories set up for temporary storage of data files while a program is running. In such cases removing those files and temporary directories is part of the cleanup before closing down the program. The recursive argument is in place to make sure that these folders do not get deleted prematurely, thereby accidentally removing files that are still needed. This is a standard part of cleaning up a program because temporary files and directories are often used.

Cleaning up directories that are no longer needed is commonly performed within a program, particularly when they are temporary directories being used by the program itself. This is a common practice in programming and is used in nearly every programming language. Now you have the tool needed to perform this operation in your programming tool belt for when you need it.

Scroll to top
Privacy Policy