I have a workspace with lots of objects and I would like to remove all but one. Ideally I would like to avoid having to type rm(obj.1, obj.2... obj.n)
. Is it possible to indicate remove all objects but these ones
?
14 Answers
Here is a simple construct that will do it, by using setdiff
:
rm(list=setdiff(ls(), "x"))
And a full example. Run this at your own risk - it will remove all variables except x
:
x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"
rm(list=setdiff(ls(), "x"))
ls()
[1] "x"

- 176,377
- 47
- 447
- 496
-
14The technique being used here is to use list= to rm, which allows a character vector to be passed to rm instead of a list of names. – Spacedman May 31 '11 at 16:14
-
3So if one wants to keep several files it would be `rm(list=setdiff(ls(), c("x", "x2")))` – boczniak767 Mar 10 '23 at 10:28
Using the keep
function from the gdata
package is quite convenient.
> ls()
[1] "a" "b" "c"
library(gdata)
> keep(a) #shows you which variables will be removed
[1] "b" "c"
> keep(a, sure = TRUE) # setting sure to TRUE removes variables b and c
> ls()
[1] "a"

- 1,595
- 14
- 13
-
1I find that using "keep" from gdata seems to make more sense and it's easier to remember that all the complexity of the comand in base R. – Darius Jan 18 '20 at 21:02
I think another option is to open workspace in RStudio and then change list to grid at the top right of the environment(image below). Then tick the objects you want to clear and finally click on clear.

- 788
- 5
- 15
-
4Likewise, click the Name box, which selects all the files, and then deselect all the files you want to keep. – Stephen Dec 27 '17 at 15:18
I just spent several hours hunting for the answer to a similar but slightly different question - I needed to be able to delete all objects in R (including functions) except a handful of vectors.
One way to do this:
rm(list=ls()[! ls() %in% c("a","c")])
Where the vectors that I want to keep are named 'a' and 'c'.
Hope this helps anyone searching for the same solution!

- 91,361
- 17
- 137
- 196

- 531
- 4
- 3
-
16This should also work, `rm(list=setdiff(ls(), c("a", "c")))`, right? See @Andrie's answer. – hplieninger Apr 29 '14 at 10:44
Replace v
with the name of the object you want to keep
rm(list=(ls()[ls()!="v"]))
hat-tip: http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

- 41,615
- 18
- 132
- 227
To keep all objects whose names match a pattern, you could use grep
, like so:
to.remove <- ls()
to.remove <- c(to.remove[!grepl("^obj", to.remove)], "to.remove")
rm(list=to.remove)

- 36,704
- 7
- 77
- 142
-
Amazing! Do you know any way to make this answer a one-line code instead of three? – Luis Nov 29 '21 at 13:06
-
2
To keep a list of objects, one can use:
rm(list=setdiff(ls(), c("df1", "df2")))

- 2,120
- 2
- 19
- 33
This takes advantage of ls()
's pattern
option, in the case you have a lot of objects with the same pattern that you don't want to keep:
> foo1 <- "junk"; foo2 <- "rubbish"; foo3 <- "trash"; x <- "gold"
> ls()
[1] "foo1" "foo2" "foo3" "x"
> # Let's check first what we want to remove
> ls(pattern = "foo")
[1] "foo1" "foo2" "foo3"
> rm(list = ls(pattern = "foo"))
> ls()
[1] "x"

- 3,942
- 2
- 28
- 26
-
whoah, that seems dangerous! Is there a way to test the pattern matching a la "echo" in the shell? – DQdlM Dec 11 '14 at 11:57
-
1I guess the `rm` function should always be used with care (more so in the shell!). See my edit for an answer to your question. – Peter Diakumis Dec 11 '14 at 13:29
require(gdata)
keep(object_1,...,object_n,sure=TRUE)
ls()

- 41
- 1
-
1Duplicate of @Rahul Premraj's [answer](http://stackoverflow.com/a/7205040/2563804). – hplieninger Mar 28 '17 at 14:03
From within a function, rm all objects in .GlobalEnv except the function
initialize <- function(country.name) {
if (length(setdiff(ls(pos = .GlobalEnv), "initialize")) > 0) {
rm(list=setdiff(ls(pos = .GlobalEnv), "initialize"), pos = .GlobalEnv)
}
}

- 31
- 2
let's think in different way, what if we wanna remove a group? try this,
rm(list=ls()[grep("xxx",ls())])
I personally don't like too many tables, variables on my screen, yet I can't avoid using them. So I name the temporary things starting with "xxx", so I can remove them after it is no longer used.

- 1,111
- 6
- 20
assuming you want to remove every object except df from environment:
rm(list = ls(pattern="[^df]"))

- 453
- 5
- 7
-
This is not correct. `[^df]` is the negation of a *set* of characters `d` and `f`. This will also remove `fdd` and `fd` and `ddd` for instance, and keep any object that has at least one instance of a character that is not `d` or `f`. – thelatemail Feb 23 '23 at 23:55
# remove all objects but selected
rm(list = ls()[which("key_function" != ls())])

- 1,086
- 14
- 17
How about this?
# Removes all objects except the specified & the function itself.
rme <- function(except=NULL){
except = ifelse(is.character(except), except, deparse(substitute(except)))
rm(list=setdiff(ls(envir=.GlobalEnv), c(except,"rme")), envir=.GlobalEnv)
}

- 313
- 1
- 4
- 14