rm()
rm(..., list = character(), envir = as.environment(pos)) Returns:
NULL (invisible) · Updated March 13, 2026 · Base Functions base objects memory environment
The rm() function removes one or more objects from the specified environment by name. It is commonly used to clean up temporary objects, free memory, or remove specific variables from the workspace.
Syntax
rm(..., list = character(), envir = as.environment(pos))
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | names | — | Object names (unquoted) to remove |
list | character | character() | Character vector of object names to remove |
envir | environment | as.environment(pos) | Environment to remove objects from |
Examples
Basic usage
# Create some objects
x <- 1:10
y <- "hello"
z <- list(a = 1, b = 2)
# List objects in the environment
ls()
# [1] "x" "y" "z"
# Remove a single object
rm(x)
ls()
# [1] "y" "z"
Removing multiple objects
# Remove multiple objects at once
rm(y, z)
ls()
# character(0)
# Create fresh objects
a <- 10
b <- 20
c <- 30
# Remove all at once using ...
rm(a, b, c)
ls()
# character(0)
Using the list parameter
# Create some objects
df <- data.frame(x = 1:5)
mat <- matrix(1:9, nrow = 3)
vec <- c(1, 2, 3)
# Remove using character vector
to_remove <- c("df", "mat")
rm(list = to_remove)
ls()
# [1] "vec"
Cleaning up from a specific environment
# Create a new environment
my_env <- new.env()
my_env$temp <- 123
my_env$data <- iris
ls(my_env)
# [1] "temp" "data"
# Remove from specific environment
rm(temp, envir = my_env)
ls(my_env)
# [1] "data"
Removing all objects except some
# Create many objects
x <- 1
y <- 2
z <- 3
keep <- "important"
# Remove all except "keep"
rm(list = setdiff(ls(), "keep"))
ls()
# [1] "keep"
Common Patterns
Clear the entire workspace
rm(list = ls())
Note: This removes all objects. A safer approach keeps hidden objects:
rm(list = ls(envir = .GlobalEnv), envir = .GlobalEnv)
Remove temporary objects after analysis
# After computation
temp_result <- sum(rnorm(1000))
# Clean up when done
rm(temp_result)
Selective cleanup in functions
process_data <- function(df) {
# Intermediate calculations
temp_mean <- mean(df$value)
temp_sd <- sd(df$value)
# Return only final result
result <- list(mean = temp_mean, sd = temp_sd)
# Clean up intermediate values
rm(temp_mean, temp_sd)
return(result)
}