[r] How do I clear only a few specific objects from the workspace?

I would like to remove some data from the workspace. I know the "Clear All" button will remove all data. However, I would like to remove just certain data.

For example, I have these data frames in the data section:

data
data_1
data_2
data_3

I would like to remove data_1, data_2 and data_3, while keeping data.

I tried data_1 <- data_2 <- data_3 <- NULL, which does remove the data (I think), but still keeps it in the workspace area, so it is not fully what I would like to do.

This question is related to r

The answer is


  1. In RStudio, ensure the Environment tab is in Grid (not List) mode.

  2. Tick the object(s) you want to remove from the environment.

  3. Click the broom icon.


Use the following command

remove(list=c("data_1", "data_2", "data_3"))

paste0("data_",seq(1,3,1)) 
# makes multiple data.frame names with sequential number
rm(list=paste0("data_",seq(1,3,1))
# above code removes data_1~data_3

You can use the apropos function which is used to find the objects using partial name.

rm(list = apropos("data_"))

If you just want to remove one of a group of variables, then you can create a list and keep just the variable you need. The rm function can be used to remove all the variables apart from "data". Here is the script:

0->data
1->data_1
2->data_2
3->data_3
#check variables in workspace
ls()
rm(list=setdiff(ls(), "data"))
#check remaining variables in workspace after deletion
ls()

#note: if you just use rm(list) then R will attempt to remove the "list" variable. 
list=setdiff(ls(), "data")
rm(list)
ls()

Following command will do

rm(list=ls(all=TRUE))

To clear all data:

click on Misc>Remove all objects.

Your good to go.

To clear the console:

click on edit>Clear console.

No need for any code.


If you're using RStudio, please consider never using the rm(list = ls()) approach!* Instead, you should build your workflow around frequently employing the Ctrl+Shift+F10 shortcut to restart your R session. This is the fastest way to both nuke the current set of user-defined variables AND to clear loaded packages, devices, etc. The reproducibility of your work will increase markedly by adopting this habit.

See this excellent thread on Rstudio community for (h/t @kierisi) for a more thorough discussion (the main gist is captured by what I've stated already).

I must admit my own first few years of R coding featured script after script starting with the rm "trick" -- I'm writing this answer as advice to anyone else who may be starting out their R careers.

*of course there are legitimate uses for this -- much like attach -- but beginning users will be much better served (IMO) crossing that bridge at a later date.


A useful way to remove a whole set of named-alike objects:

rm(list = ls()[grep("^tmp", ls())])

thereby removing all objects whose name begins with the string "tmp".

Edit: Following Gsee's comment, making use of the pattern argument:

rm(list = ls(pattern = "^tmp"))

Edit: Answering Rafael comment, one way to retain only a subset of objects is to name the data you want to retain with a specific pattern. For example if you wanted to remove all objects whose name do not start with paper you would issue the following command:

rm(list = grep("^paper", ls(), value = TRUE, invert = TRUE))