Unload Package without Restarting R (Example)


This tutorial explains how to unload a package without restarting R (or RStudio).

The article is structured as follows:

Here’s the step-by-step process:


Step 1: Install & Load Package in R

I’m going to use the stringr package for the example of this R tutorial. Let’s install and load the package to R:

install.packages("stringr")                     # Install stringr package
library("stringr")                              # Load stringr package

The package contains many add-on functions for the manipulation of character strings in the R programming language. For instance, we can use the str_sub function to subset certain characters of a character string:

str_sub(string = "ABCDE", start = 2, end = 4)   # Apply str_sub function
# "BCD"

Works fine. Now let’s assume we don’t need the package anymore and for that reason we want to remove the package from our R session…


Step 2: Unload Package without Restarting R

If we want to unload the stringr package (or any other CRAN package), we can apply the detach command:

detach("package:stringr")                       # Detach stringr package

Note that within the detach function we have to write package: in front of the name of the package (i.e. stringr).

Now, let’s see whether the detaching worked. If we apply the str_sub function again, the following happens:

str_sub(string = "ABCDE", start = 2, end = 4)   # Apply str_sub function


error message because package is not loaded to r

Figure 1: Error Message after Unloading stringr Package.


The function does not exist anymore. We successfully removed the stringr package from our R session!


Video & Further Resources

In case you need further explanations on the removal of packages from an R session, you may have a look at the following video that I have recently published on my YouTube channel. In the video, I’m explaining the example of this tutorial in more detail:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you could have a look at the other R tutorials of this website. Some interesting tutorials can be found below:

At this point of the article, you should know how to detach a package without closing/shutting down and reopening R (or RStudio). In case you have any further questions, please let me know in the comments section below.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

4 Comments. Leave new

    January 28, 2022 1:47 pm

    When I use
    I am getting an error message as follows. Kindly guide

    Error in detach(“package:sna”) : invalid ‘name’ argument

    • Hey Shrinivas,

      Do you also get the error message when you run the code like that?



    May 28, 2022 5:24 am

    Hello Jo

    I may or may not get the error message the way you have suggested. But it does not help me.
    I cannot every time go through this process.
    I wish to detach a package in the process

    • Hey Shrinivas,

      I’m sorry for the delayed reply, I just came back from vacation. Do you still need help with this error?



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.