How can I remove all installed packages except base
and recommended
?

- 10,297
- 18
- 80
- 121
-
2Just out of curiosity, why would one want to do this instead of fresh R installation? – CHP May 05 '13 at 11:00
-
1Updated to R 3.0.0 and have to rebuild all packages. Some give errors so I thought revert to vanilla and reinstall. – ECII May 05 '13 at 11:57
-
2@geektrader that is the question I *should* have asked before attempting to answer! :-) – Simon O'Hanlon May 05 '13 at 12:56
6 Answers
Be CAREFUL! And read the docs before you try this:
# Pasted as a commented to prevent blindly copying and pasting
# remove.packages( installed.packages( priority = "NA" )[,1] )
By default this will remove packages from the first library in your .libPaths()
. Otherwise you can specify the following argument to remove.packages()
:
, lib = .libPaths()[1]

- 186
- 15

- 58,647
- 14
- 142
- 184
-
1
-
Assuming default install settings and default use of install. packages and nothing fancy in. REnviron file or .RProfile file then I think so! :-) – Simon O'Hanlon May 05 '13 at 10:06
-
2I just ran this code for R 3.0.0 and here are the only packages still available: base, boot, class, cluster, codetools, compiler, datasets, foreign, graphics, grDevices, grid KernSmooth, lattice, MASS, Matrix, methods, mgcv, nlme, nnet, parallel, rpart, spatial, splines, stats, stats4, survival, tcltk, tools, utils – chandler Nov 23 '14 at 20:05
-
1I just ran the code and got `Error in find.package(pkgs, lib) : there are no packages called ‘assertthat’, ‘backports’, ‘brew’, ‘callr’, ‘cli’, ‘clipr’, ‘clisymbols’, ‘commonmark’, ‘crayon’, ‘curl’, ‘desc’, ‘devtools’, ‘digest’, ‘evaluate’, ‘fansi’, ‘fs’, ‘gh’, ‘git2r’, ‘glue’, ‘highr’, ‘httr’, ‘ini’, ‘jsonlite’, ‘knitr...`, How could I solve this? – Cris Apr 06 '19 at 12:35
Instead of
Updated to R 3.0.0 and have to rebuild all packages.
just do
update.packages(..., checkBuilt=TRUE)
which is what I did on my R 3.0.0 (using lib.loc=...
to point to my different local directories). This will update everything you have and which it can still get from repos such as CRAN. For install_git()
etc, you are out of luck and need to reinstall.
But either way you do not need to remove the packages first.

- 360,940
- 56
- 644
- 725
-
I did `update.packages()` without the `checkBuilt=T` and run into errors. Was that the cause of my probs? – ECII May 05 '13 at 12:13
-
1Well if a package no longer exists, or builds, or ... it will not work under this, but neither will it when you manually install it. This just makes your life easier. YMMV. – Dirk Eddelbuettel May 05 '13 at 12:15
-
2@ECII Yes. `checkBuilt` checks if packages were built under the previous major release of R and marks them as old and in need of updating if TRUE, and will *try* to update them. – Simon O'Hanlon May 05 '13 at 12:56
Accepted answer no longer works (R 3.6.X), but this one does:
update.packages(checkBuilt = T, ask = F)
We use checkBuilt=T
because this checks whether packages were built under an older version and need to be rebuilt (sometimes).
We use ask=F
because otherwise we get a prompt for each package which is annoying.

- 6,219
- 5
- 59
- 89
Here is a solution available in the R-Blogger:
# create a list of all installed packages
ip <- as.data.frame(installed.packages())
head(ip)
# if you use MRO, make sure that no packages in this library will be removed
ip <- subset(ip, !grepl("MRO", ip$LibPath))
# we don't want to remove base or recommended packages either\
ip <- ip[!(ip[,"Priority"] %in% c("base", "recommended")),]
# determine the library where the packages are installed
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
# remove the packages
sapply(pkgs.to.remove, remove.packages, lib = path.lib)
Here is the link for the original post: https://www.r-bloggers.com/how-to-remove-all-user-installed-packages-in-r/

- 18,094
- 55
- 145
- 232
If on Linux, the easiest thing is probably to remove the library folder, which by default is located in /home/yourusername/R
.
On Fedora, for example, it is called x86_64-redhat-linux-gnu-library
.
If the folder /home/yourusername/R/x86_64-redhat-linux-gnu-library
is deleted, it is automatically recreated at the following start of R. All default libraries are regularly available.

- 3,557
- 2
- 20
- 29

- 3,043
- 21
- 24
-
*it is automatically recreated at the following start of R* → **no** – it is only recreated once you ask for installing a package, and the system library is not writable for your user. – slhck Jan 04 '19 at 16:55
WARNING, YOU WILL DELETE A LOT OF STUFF
Sometimes uninstalling packages does not work, in which case you may want to delete the folder of the packages. This can be done from R assuming you have permissions.
sapply(paste(installed.packages( priority = "NA" )[, 2], installed.packages( priority = "NA" )[, 1], sep = "/"), unlink, recursive = T)
You can preview the paths to be delete by:
sapply(paste(installed.packages( priority = "NA" )[, 2], installed.packages( priority = "NA" )[, 1], sep = "/"), identity)
This call:
- Gets the list of installed non-base packages
- Makes a vector of their installed paths
- Loops over the paths
- Deletes each folder recursively

- 6,219
- 5
- 59
- 89