How to Clear the Global Environment in R

R console showing commands for removing objects from the global environment

Use rm() to remove selected objects or clear the objects listed in R’s global environment. A targeted command is safer when you need to preserve some work; a full rm(list = ls()) clears the ordinary, visible objects in the current environment.

How to clear the global environment in R with rm(list = ls())

Run this command at the Global Environment prompt:

rm(list = ls())

It removes every object returned by ls() in that environment. Check the contents before and after to confirm the result:

ls()
[1] “data” “model” “results”

rm(list = ls())

ls()
character(0)

The result character(0) means that no visible objects remain. The command is evaluated in the current environment, so run it from the Global Environment if that is the environment you intend to clear.

By default, ls() does not list hidden names beginning with a period. To include those names in a more comprehensive cleanup, use:

rm(list = ls(all.names = TRUE))

Use the extended form only when you also intend to remove hidden objects managed or created in that environment.

Clear environment in R selectively with rm(object) and rm(list = c(‘data’, ‘model’))

Remove one object by passing its name to rm():

object <- 42
rm(object)

Verify that the object is gone:

ls()
character(0)

For a real workspace, inspect the names first and remove only the objects you no longer need:

ls()
[1] “data” “model” “results”

rm(list = c(‘data’, ‘model’))

ls()
[1] “results”

The names supplied to rm(list = c(…)) must be character strings. If a name does not exist, R can report an error. Check names with ls() first, or use rm(data, model) when the objects are known to exist.

Clear data in R: what rm() removes and what it does not

In R, a data frame, vector, list, function, or fitted model is an object. Removing it deletes its binding from the selected environment:

rm(data)

This does not delete an original CSV file, database table, or other external source used to create the object. It also does not remove individual columns from a data frame. To change the data frame itself, assign a revised object or remove a column with a separate operation.

Removing a large object makes it eligible for memory cleanup, but it does not restart R or necessarily return memory to the operating system immediately. Use targeted removal when you want to keep analysis results, imported data, or model objects.

Verify with ls() or objects(): what packages, plots, and session state remain?

objects() is an alternative to ls() for listing objects. Use either command after removal:

objects()
character(0)

Clearing objects does not detach loaded packages. Functions from attached packages remain available, and package namespaces are not unloaded. It also does not close graphics devices: a displayed plot remains on its device until you close it with a graphics command such as dev.off(). A plot stored as an object, however, is removed if its name is included in the rm() call.

Working-directory settings, options, open connections, and the R session itself also remain. Clearing the global environment is therefore not a complete R session restart. Restarting R is a separate action that ends and starts the session again.