8

I tried this command

remove(list = ls())

I expect to clear all R environment (Objects, packages)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohamed SELAMA
  • 96
  • 1
  • 1
  • 4

3 Answers3

14

The simplest and, more importantly, the only reliable way of doing this is to restart R. That takes care of everything.

Just make sure you’re not accidentally saving the current R image when quitting R.

In RStudio, you need to set the option “Save workspace to .RData file on exit” to “Never”, and disable restoring upon restart — this is strongly recommended!

RStudio preferences

After that, make sure that any previously existing .RData files in your project’s folder are deleted (heads up: .RData is an invisible file so you won’t normally see it in a file browser; you can delete it via the command line).

To restart R from within RStudio, you can use “Session” › “Restart R” or Cmd+Shift+F10.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

The answer was already out there :-) https://stackoverflow.com/a/7506112/7902133

According to this answer, the following code should work

lapply(paste("package:", names(sessionInfo()$otherPkgs), sep=""), 
       detach, 
       character.only = TRUE, 
       unload = TRUE)

You may also want to check the first answer for a full description.

Alfonso
  • 644
  • 7
  • 17
  • 1
    If the question has already an answer elsewhere it should be flagged as duplicate instead of answering it. Please read the FAQ [How should duplicate questions be handled?](http://meta.stackexchange.com/q/10841/): "It's a duplicate. What do I do?; If you have the privilege to vote to close, click the "close" button under the question, select “duplicate of...”, and paste a link to the duplicate question."; "Should I answer it?; No, not if you think it's a duplicate." – Ronak Shah Jul 17 '19 at 09:40
  • 2
    This will not work reliably if there are dependencies between different loaded packages. Even apart from that it might not reliably reset all session state. – Konrad Rudolph Jul 17 '19 at 09:45
  • This "solution" just doesn't answer the original question. – tagoma Sep 23 '21 at 21:26
0

The freshr package has consolidated the previous answers into one simple function. Install it via

install.packages("freshr")

and then run

freshr::freshr()

in your console and it will unload all packages and variables for you.

Shawn Lin
  • 17
  • 2
  • 1
    Frankly, this is a bad approach that attempts to poorly solve a non-exist problem. First off, this package *does not actually work*. It only attempts to unload a *tiny subset* of all loaded packages (`sessionInfo()$otherPkgs` is not complete!), and it may fail if a package cannot be unloaded because another package depends on it. It also completely ignores any non-package code that might require unloading (which is good, because there is no general solution for dealing with such code!). And, as explained in the other answer, this package is completely unnecessary anyway: just restart R. – Konrad Rudolph Feb 10 '23 at 08:44
  • 1
    … worst of all, by recommending to put `freshr::freshr()` at the top of every script *this package actively promotes poor engineering practices*. – Konrad Rudolph Feb 10 '23 at 08:46