Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Remove objects from the environment (rm / remove)


Lesson Description
-
  • When experimenting, we often want to remove temporary objects from the environment to avoid name clashes and stale values
  • ls() (or objects()) lists what is currently in the environment
  • rm(x) removes a single object; rm(a, b) removes multiple
  • For scripts, prefer rm(list = ...) where the list is built programmatically
  • Common patterns
    • Keep only a few: rm(list = setdiff(ls(), c("adsl","adlb")))
    • Remove by pattern: rm(list = ls(pattern = "^tmp_"))
    • Clear all (use with care): rm(list = ls())
  • Think of this like cleaning the SAS WORK library between steps—keeps our workspace predictable while we iterate
#==============================================================================
# Remove objects from the workspace (Beginner → Intermediate)
#==============================================================================

#==============================================================================
# List objects currently in the global environment (like SAS PROC CONTENTS for WORK)
#==============================================================================
ls()            # or objects()

#==============================================================================
# Remove a single object by name
#==============================================================================
x <- 1
rm(x)           # 'x' is now removed
"x" %in% ls()   # FALSE

#==============================================================================
# Remove multiple objects
#==============================================================================
a <- 10; b <- 20; c <- 30
rm(a, b)
ls()

#==============================================================================
# Remove objects by character vector
#==============================================================================
to_drop <- c("b", "c")
rm(list = to_drop)

#==============================================================================
# Keep only selected objects (drop everything else)
#==============================================================================
keep <- c("adsl", "adlb")       # example object names we want to keep
rm(list = setdiff(ls(), keep))

#==============================================================================
# Remove objects matching a name pattern
#==============================================================================
tmp_01 <- 1; tmp_02 <- 2; tmp_final <- 3
rm(list = ls(pattern = "^tmp_\d+$"))  # removes tmp_01 and tmp_02, keeps tmp_final

#==============================================================================
# Clear the entire environment (use with care)
#==============================================================================
# Equivalent to starting a fresh WORK library in SAS mid-session
rm(list = ls())

#==============================================================================
# Tips
#==============================================================================
# 1) Use ls() to preview what will be removed before calling rm()
# 2) Prefer explicit lists (rm(list = ...)) for reproducible scripts
# 3) In RStudio, you can also click the 'broom' in Environment pane to clear all
# Remove objects from the workspace

# List objects currently in the global environment
ls()

# Remove a single object by name
x <- 1
rm(x)
"x" %in% ls()

# Remove multiple objects

a <- 10
b <- 20
c <- 30

rm(a, b)
ls()

# Remove objects by character vector

to_drop <- c("b", "c")
rm(list = to_drop)

# Keep only selected objects (drop everything else)

keep <- c("adsl", "adlb")
rm(list = setdiff(ls(), keep))

# Remove objects matching a name pattern

tmp_01 <- 1

# tmp_02 <- 2
# tmp_final <- 3
rm(list = ls(pattern = "^tmp_\\d+$"))

# Clear the entire environment (use with care)
rm(list = ls())
  • rm() deletes objects; ls() lists them.
  • rm(list = ...) removes multiple objects, including by pattern.
  • setdiff(ls(), keep) helps keep only selected objects.