Re: [R] Removing objects and clearing memory

From: Rolf Turner <>
Date: Wed, 13 Apr 2011 10:45:37 +1200

On 13/04/11 09:49, Denis Kazakiewicz wrote:
> On 13.04.2011 00:39, Jim Silverton wrote:

>> How do I remove all objects except one in R?

> rm(list=ls()) #will remove ALL objects

But he wanted to remove all objects ***except one***!!! So what's the point of this answer?

I can't see any way except a rather round-about kludge to do what the OP wants.

Create a function:

rmabo <- function(x) {
# Remove all but one.

     fff <- tempfile()


Save this function (otherwise it will get removed when you remove all objects except one, and rmabo is not the one, and will be lost):


Whenever you want to remove all objects except one, attach the data base in which you have saved "rmabo". (You only need to do the attaching once per session; if you like you could put the attach command in your .Rprofile.)


Then to remove all objects except "melvin" execute rmabo(melvin). E.g.:

melvin <- clyde <- irving <- fred <- 42
[1] "clyde" "fred" "irving" "melvin"
[1] "melvin"

This seems to work. Anyone have any cleverer ideas to accomplish the stated objective?


         Rolf Turner mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 12 Apr 2011 - 22:50:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Apr 2011 - 23:20:34 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive