Quick summary
Summarize this blog with AI
Introduction
Removing objects, clearing the global workspace and restarting R have different effects. rm() removes bindings from an environment; it does not unload every package, reset options, close every connection, or reproduce a fresh process. Start with selective removal, and save any work you need before a broader cleanup.
Tested with R 4.5.1. Run the code blocks in order. Install the named packages before starting; inputs are supplied below.
Inspect and remove selected objects safely
Use a separate demonstration environment so the first examples cannot delete objects from your working session. Hidden names start with a dot and are omitted by ls unless all.names is TRUE.
workspace <- new.env(parent = emptyenv())
workspace$result <- 42
workspace$temp <- 10
workspace$.hidden <- "keep until reviewed"
stopifnot(identical(sort(ls(workspace)), c("result", "temp")))
print(sort(ls(workspace, all.names = TRUE)))
# ".hidden" "result" "temp"
rm(list = "temp", envir = workspace)
stopifnot(!exists("temp", envir = workspace, inherits = FALSE), workspace$result == 42)
The envir argument makes the target explicit. Removing a binding does not delete files on disk. If another binding still references the same object, that reference remains.
Use the same environment for listing and removal
Inside a function, a bare ls() lists that function’s evaluation environment. Passing those names to rm in another environment is a scope error.
clear_environment <- function(target) {
stopifnot(is.environment(target))
rm(list = ls(envir = target, all.names = TRUE), envir = target)
invisible(NULL)
}
clear_environment(workspace)
stopifnot(length(ls(workspace, all.names = TRUE)) == 0L)
clear_environment(workspace) # empty removal is harmless
parent <- new.env(parent = emptyenv()); parent$shared <- 7
child <- new.env(parent = parent); child$local <- 8
clear_environment(child)
stopifnot(!exists("local", child, inherits = FALSE), parent$shared == 7)
This corrects the original .First pattern, which listed a local frame but targeted globalenv(). Avoid placing destructive cleanup in startup hooks; it can discard restored work without making the session reproducible.
Clear the global workspace only deliberately
The next block really removes all global objects, including hidden names. Run it only in this disposable lesson session after saving anything needed. It is last because earlier fixture objects will be gone.
options(sqlpad_cleanup_demo = TRUE)
rm(list = ls(envir = globalenv(), all.names = TRUE), envir = globalenv())
stopifnot(length(ls(envir = globalenv(), all.names = TRUE)) == 0L)
stopifnot(isTRUE(getOption("sqlpad_cleanup_demo")))
options(sqlpad_cleanup_demo = NULL)
cat("Global objects removed; the option survived until explicitly removed.\n")
The surviving option proves that clearing objects is not a full reset. Garbage collection may reclaim unused memory later, but gc() is also not a session restart.
Restart and rerun to check reproducibility
In RStudio, use Session → Restart R, then rerun the saved script from its first line. Check project/startup settings that may restore .RData or execute .Rprofile. Do not equate the editor’s Save command with saving a workspace.
For a reproducibility check from a terminal, Rscript --vanilla analysis.R starts a fresh process and skips normal startup/restore settings. It does not freeze package versions, input files or environment variables. Record those separately. Prefer scripts plus explicitly saved outputs such as saveRDS over relying on a hidden restored workspace; never load an untrusted R workspace as if it were inert text.