How to Remove User-Defined Objects From the Workspace in R

Jesse John Feb 02, 2024
  1. Use the rm() Function to Remove User-Defined Objects From the Workspace in R
  2. Use the R Studio Interface to Remove User-Defined Objects From the Workspace in R
  3. Conclusion
How to Remove User-Defined Objects From the Workspace in R

Our data analysis may create many objects that take up memory. This article shows how to remove user-defined objects from the workspace in R using the rm() function and the Environment tab of R Studio.

Use the rm() Function to Remove User-Defined Objects From the Workspace in R

In our R script, we can use the rm() or remove() functions to remove objects. Both functions are identical.

We can specify the objects to remove as variables, strings, or a character vector supplied to the list argument.

Example code:

# Make several objects.
a = 1:10
b = 11:20
c = c(a,b)
df1 = data.frame(a, b)
x = a
y = b
z = c
df2 = data.frame(x, y)
df3 = cbind(df1, df2)

# Lists the objects in the top level (global) environment.
ls()

# Remove one object.
# Specify as a variable.
rm(a)
# Check the list.
ls()

# Remove another object.
# Specify as a string.
rm("b")
# Check the list.
ls()

# Remove multiple objects as variables.
rm(x, y)
# Check the list.
ls()

# Remove multiple objects as strings.
rm("df1", "df2")
# Check the list.
ls()

# Remove multiple objects by providing a character vector to list.
rm(list = c("c", "z"))
# Check the list.
ls()

Output:

> # Lists the objects in the top level (global) environment.
> ls()
[1] "a"   "b"   "c"   "df1" "df2" "df3" "x"   "y"   "z"
>
> # Remove one object.
> # Specify as a variable.
> rm(a)
> # Check the list.
> ls()
[1] "b"   "c"   "df1" "df2" "df3" "x"   "y"   "z"
>
> # Remove another object.
> # Specify as a string.
> rm("b")
> # Check the list.
> ls()
[1] "c"   "df1" "df2" "df3" "x"   "y"   "z"
>
> # Remove multiple objects as variables.
> rm(x, y)
> # Check the list.
> ls()
[1] "c"   "df1" "df2" "df3" "z"
>
> # Remove multiple objects as strings.
> rm("df1", "df2")
> # Check the list.
> ls()
[1] "c"   "df3" "z"
>
> # Remove multiple objects by providing a character vector to list.
> rm(list = c("c", "z"))
> # Check the list.
> ls()
[1] "df3"

We can remove all user-defined objects at once by providing the output of the ls() function to the list argument of the rm() function.

The documentation specifies that the action will be performed without warning and suggests that users should not use it unless they are sure.

Example code:

# Again create some objects.
A = 100:110
B = 200:210
DF1 = data.frame(A, B)

# Check the list of objects.
ls()

# Remove all user-defined objects.
rm(list = ls())
# Check the list of objects.
ls()

Output:

> # Check the list of objects.
> ls()
[1] "A"   "B"   "DF1" "df3"
>
> # Remove all user-defined objects.
> rm(list = ls())
> # Check the list of objects.
> ls()
character(0)
Note
These functions do not detach packages that we load using the library() function. We can use the detach() function to detach packages we no longer need.

Use the R Studio Interface to Remove User-Defined Objects From the Workspace in R

R Studio makes it very convenient to remove user-defined objects from memory.

The top-right panel of the R Studio interface has an Environment tab, where all user-defined objects are listed.

In the first row of icons, notice the broom icon in the middle and the Grid or List drop-down selector at the far end, just before the refresh button. We need to select Grid to control which objects we remove from the workspace.

In the second row of icons, notice the label Global Environment.

When we choose Grid in the first row of icons, we get column names. Notice the selection box right before all column names and the selection boxes for each object in the list.

R Studio Environment tab

We can remove objects by clicking the broom icon. The different options available are as follows.

  • Clicking the broom icon when no objects are selected will remove all user-defined objects, including hidden objects (a selector appears in the confirmation dialog).

  • Selecting one or more objects and clicking the broom icon will remove the selected object(s).

  • Selecting all objects by clicking the selector in the list of column names and then clicking the broom icon will remove all the objects which are visible (and selected) in the list.

  • To keep one or more objects and remove all others, we can unselect those manually, or we can do the following:

    • Click the selector in the row of column names to select all objects.
    • Unselect the objects we want to keep.
    • Click the broom icon. Only the unselected objects will remain.

Conclusion

We can use the rm() function to remove user-defined objects from the workspace. R Studio makes this task easier with its Environment tab.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R Object