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"
2 Comments
rm(list=setdiff(ls(), c("x", "x2")))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.

1 Comment
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"
2 Comments
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!
1 Comment
rm(list=setdiff(ls(), c("a", "c"))), right? See @Andrie's answer.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)
2 Comments
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
Comments
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"
2 Comments
rm function should always be used with care (more so in the shell!). See my edit for an answer to your question.require(gdata)
keep(object_1,...,object_n,sure=TRUE)
ls()
1 Comment
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.
Comments
assuming you want to remove every object except df from environment:
rm(list = ls(pattern="[^df]"))
1 Comment
[^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.