Written and maintained by CASRAI Editorial Board
Published
On this page
To uninstall a package in R, run remove.packages("packagename") in the R console. The function removes the installed package and updates R’s index information; if you do not name a library, it works on the first directory returned by .libPaths(). To uninstall several packages at once, pass a character vector, for example remove.packages(c("pkgA", "pkgB")). In RStudio, the X icon in the Packages pane sends the same call to the console.
This guide goes past the one-line answer. It shows how to find which library a package actually sits in before you remove it, how uninstalling differs from detaching and unloading, what to check before you remove a package that other packages use, and which free methods exist. Every function and argument below comes from the R manual pages and the Posit RStudio documentation listed under Sources.
What uninstalling a package means in research work
Analysis projects accumulate packages: a statistics package tried once, a version that conflicts with another, a package installed into the wrong library. Removing a package you no longer use keeps the set of installed software small and easier to describe, which matters when you document the computing environment behind a result (see computational reproducibility). It is also a common step when a package misbehaves and you want a clean state before installing it again, as described in the guide to updating R.
Three different actions are often confused, and the R documentation treats them separately:
- Uninstalling removes the installed package from a library directory (
remove.packages()). - Detaching takes a package off the search path of the current session (
detach()), without removing it from disk. - Unloading removes a package’s namespace from the current session;
detach()can attempt it when you setunload = TRUE.
Only uninstalling changes what is installed. The other two affect the running session only, and a package you detach is available again with library() in the same or a later session.
Install or access: where the function lives
remove.packages() is part of the utils package, which is attached in a standard R session, so you do not install or load anything first. Its help page is titled “Remove Installed Packages” and describes the function as removing installed packages/bundles and updating index information as necessary. You open it from any R console with ?remove.packages. The companion function for installing, install.packages(), is named in the same help page under “See also”, together with REMOVE, the command-line version for Unix-alikes.
Step-by-step: uninstall a package in R
1. Check what is installed and where
The documented signature of installed.packages() is installed.packages(lib.loc = NULL, ...). It returns a matrix with one row per package and the columns Package, LibPath, Version, Priority, Depends, Imports, LinkingTo, Suggests, Enhances, OS_type, License and Built. With lib.loc = NULL it looks in all known library trees, so one call shows you every copy of a package on the machine:
ip = installed.packages()
ip[, c("Package", "LibPath", "Version")]
The LibPath column is the key detail. If a package appears under a directory that is not your first library, remove.packages() will not find it unless you say where to look (step 3).
2. See which libraries R searches
Calling .libPaths() with no arguments returns the library trees R searches, as a character vector. The first element is the default target of remove.packages(). The libraries come from the environment variables R_LIBS, R_LIBS_USER and R_LIBS_SITE at startup; entries are separated by a colon on Unix-alikes and a semicolon on Windows. R_LIBS_USER defaults to a version-specific folder in the user’s home area (under LOCALAPPDATA on Windows), and since R 4.2.0 the startup process sets both user and site variables if they are unset or empty.
.libPaths()
3. Run remove.packages()
The documented usage is remove.packages(pkgs, lib). The argument pkgs is a character vector of the names of the packages to remove. The argument lib is a character vector of library directories to remove from; if you omit it, the default is the first element of .libPaths().
remove.packages("ggplot2")
Use the exact package name as a quoted string. When the package sits in a library other than the first one, give that directory explicitly, copying the path from the LibPath column of step 1:
remove.packages("ggplot2", lib = "path/to/the/library")
4. Uninstall several packages in R at once
Because pkgs is a character vector, one call can remove many packages. This is the answer to “uninstall packages in R” in the plural:
remove.packages(c("pkgA", "pkgB", "pkgC"))
If the packages are spread over several libraries, lib also accepts a vector of directories, so you can list each one.
5. Confirm it is gone
Run installed.packages() again and check that the package no longer appears in the matrix, or look for it by row name, for example "ggplot2" %in% rownames(installed.packages()), which returns FALSE once the package is removed from every library R knows about.
Uninstall a package in RStudio
RStudio does not use a separate mechanism. In the Packages pane, the final column of each row holds an X icon that, according to the Posit documentation, can be used to uninstall a package. Clicking it uninstalls the package via a remove.packages() invocation sent to the R console, so you will see the call echoed there. The first column holds a checkbox that either loads or unloads the package as appropriate, which is a session action and not an uninstall.
Because the pane issues the same call, it has the same behaviour: it acts on the library R resolves by default unless the call names another. If you want to be certain which directory is affected, run the check in steps 1 and 2 in the console and use the console command with an explicit lib.
Detach, unload and uninstall: the documented limits
A frequent reason to want to uninstall is that a package seems “stuck” in a running session. The detach() help page explains why detaching is not a substitute for uninstalling and why it is sometimes incomplete.
The signature is detach(name, pos = 2L, unload = FALSE, character.only = FALSE, force = FALSE). With the default unload = FALSE the package is only removed from the search path. With unload = TRUE, R attempts to unload the namespace as well, through unloadNamespace. The documentation states that if the namespace is imported by another namespace, or if unload is FALSE, no unloading will occur. It also notes that detaching does not unload DLLs or remove registered S3 methods, and that the most reliable way to completely detach a package is to restart R.
detach("package:ggplot2", unload = TRUE)
In practice, a sensible order is: detach (and unload, if you want to try), restart R if anything remains loaded, then call remove.packages() in a fresh session. Starting a fresh session is the documented reliable route to a fully detached package, and it also resets any state you built up in the old session.
Before you remove: check what depends on the package
The help page for remove.packages() lists only two arguments, pkgs and lib; it has no argument for checking other packages that rely on the one you are removing. The check is therefore yours to make. The installed.packages() matrix includes the Depends, Imports and LinkingTo columns, which record what each installed package declares it needs. A rough check is to search the Imports column for the package name:
ip = installed.packages()
ip[grepl("ggplot2", ip[, "Imports"]), "Package"]
This pattern match is approximate, since it also matches longer names that contain the text, and you can repeat it for the Depends and LinkingTo columns. Any package it lists declares a need for the one you plan to remove, so removing it may break those packages until it is installed again. If a package is needed for a published analysis, reinstall it with install.packages(), the function the manual names alongside remove.packages().
Output and how to read it
remove.packages() is a command that acts rather than returns a result you analyse, so the useful output is the state afterwards. A call that succeeds leaves the package out of installed.packages(). In RStudio, the Packages pane refreshes its list and the console shows the call that was sent. Reading the matrix from step 1 is the dependable way to see what changed: compare the Package, LibPath and Version columns before and after, and note any other library that still holds a copy of the same package.
Troubleshooting
The package is still listed after removal
Run installed.packages()[, c("Package", "LibPath")]. If the package appears under a second library, the first call acted on the default library only. Repeat the removal with lib set to that second path. This follows directly from the documented default, which is the first element of .libPaths().
The package still seems loaded
Uninstalling changes what is installed, not what is already loaded in your session. Detach it, restart R, and check again; the manual describes restarting R as the most reliable way to completely detach a package.
Unloading does nothing
If detach(..., unload = TRUE) leaves the namespace loaded, the documented explanation is that another namespace imports it. Restart R rather than forcing the issue.
A different package stopped working afterwards
That package probably declared the removed one in Depends, Imports or LinkingTo. Reinstall the removed package with install.packages(). For the broader picture of keeping R and its packages in step, see the guide to updating R.
You are working in a shared or project library
The default target is the first path from .libPaths(), which depends on the environment variables set at startup. Check .libPaths() in the same session where you run the removal, since a project or startup file can change it.
Free alternatives and related methods
All of these are free and part of R or RStudio. The table lists only what the documentation states.
| Method | Where you use it | What the documentation says |
|---|---|---|
remove.packages() |
R console or script | Removes installed packages and updates index information; lib defaults to the first element of .libPaths(). |
| Packages pane X icon | RStudio | Uninstalls the package via a remove.packages() invocation sent to the R console. |
REMOVE |
Command line, Unix-alikes | Named on the help page as the command-line version of package removal. |
detach() |
R console or script | Takes a package off the search path; unloading is attempted only with unload = TRUE and is blocked if another namespace imports the package. |
| Restart R | Any R session | The manual’s most reliable way to completely detach a package. |
The data table below the guide sets out the four base functions used here, with their key arguments and documented defaults. If you are choosing a tool for analysis more broadly, the comparison of R and Stata for statistical analysis covers that decision, and the guide to changing the working directory in R covers another common session-setup task.
A worked example: removing a package from a second library
This is an illustrative sequence with a placeholder package name, not a recorded session. Suppose you installed a package called examplepkg and later want it gone, but a first call has no effect on the listing.
- Run
.libPaths(). It returns two directories; the first is a user library and the second a site library. - Run
installed.packages()[, c("Package", "LibPath", "Version")]and filter for the rowexamplepkg. Its LibPath shows the second directory. - Run
remove.packages("examplepkg"). Sincelibwas omitted, R targets the first library, where the package is not installed. - Run
remove.packages("examplepkg", lib = .libPaths()[2]), naming the second directory that step 2 showed. - Run
installed.packages()again and confirmexamplepkgis absent.
The lesson is the documented default: without lib, the call acts on the first library only, so check LibPath first whenever you use more than one library.
Frequently asked questions
How do I uninstall a package in R?
Run remove.packages("packagename") in the console. According to the R manual it removes the installed package and updates index information, using the first library in .libPaths() unless you pass lib.
How do I uninstall packages in R when I have several to remove?
Pass a character vector to pkgs: remove.packages(c("pkgA", "pkgB")). The manual defines pkgs as a character vector of package names.
How do I uninstall a package in RStudio?
Use the X icon at the end of the package’s row in the Packages pane. The Posit documentation says this uninstalls the package via a remove.packages() invocation sent to the R console.
What is the difference between detach and uninstall?
Detaching removes a package from the session’s search path; uninstalling removes it from the library. Detaching with unload = TRUE may not unload a namespace that another namespace imports, and the manual recommends restarting R for a complete detach.
How do I know which library a package is in?
Call installed.packages() and read the LibPath column, then compare with .libPaths(), which returns the library trees R searches.
Will removing a package break other packages?
It can, if they declare it in Depends, Imports or LinkingTo. The remove.packages() page has no dependency-check argument, so inspect those columns of installed.packages() first.
How do I put the package back?
Use install.packages(), which the remove.packages() help page lists under “See also”.
Sources
- R Documentation: remove.packages, utils package, R-devel manual. https://stat.ethz.ch/R-manual/R-devel/library/utils/html/remove.packages.html (checked 2026-11-14)
- R Documentation: installed.packages, utils package. https://stat.ethz.ch/R-manual/R-devel/library/utils/html/installed.packages.html (checked 2026-11-14)
- R Documentation: detach, base package. https://stat.ethz.ch/R-manual/R-devel/library/base/html/detach.html (checked 2026-11-14)
- R Documentation: .libPaths, base package. https://stat.ethz.ch/R-manual/R-devel/library/base/html/libPaths.html (checked 2026-11-14)
- Posit: RStudio User Guide, Packages pane. https://docs.posit.co/ide/user/ide/guide/ui/packages-pane.html (checked 2026-11-14)
Data: Base R functions used to find and uninstall an installed package
Compares the four base and utils R functions used to locate, inspect and remove installed packages: what each does, its key argument and that argument's documented default. Compiled from the R-devel manual pages for remove.packages, installed.packages, detach and .libPaths, read 2026-11-14.
| Function | Package | Purpose (per manual) | Key argument | Documented default |
|---|---|---|---|---|
| remove.packages | utils | Removes installed packages/bundles and updates index information as necessary | lib | First element of .libPaths() |
| installed.packages | utils | Returns a matrix with one row per installed package (columns include Package, LibPath, Version, Imports) | lib.loc | NULL (all known library trees) |
| .libPaths | base | With no arguments, returns the library trees R searches | (none for the query form) | Built at startup from R_LIBS, R_LIBS_USER and R_LIBS_SITE |
| detach | base | Removes a package from the search path; can attempt to unload its namespace | unload | FALSE (no unloading) |
Sources
- R manual: remove.packages (accessed 2026-11-14)
- R manual: installed.packages (accessed 2026-11-14)
- R manual: detach (accessed 2026-11-14)
- R manual: .libPaths (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







