R keeps every object you create in memory until you explicitly remove it. Over a long session, or inside a script that builds many intermediate data frames, that habit quietly fills up RAM and slows everything down. Removing unused objects frees that memory and makes your workspace easier to reason about. This guide walks through the safe way to identify what is no longer needed, delete it, and confirm the cleanup worked.
What You’ll Need
- An R session (RStudio or the console) with the objects you want to clean up already loaded.
- A basic idea of which object names your script still uses later on.
- Optional: the
pryrpackage for memory reporting, though base R is enough for the core steps.
Step-by-Step
- List everything in your workspace. Run
ls()to see the names of all objects in the global environment. For a compact view of name, class, and size, runls.str(). This is your inventory; anything you do not recognise is a candidate for removal. - Check what each object is before deleting it. Use
class()andstr()on anything unfamiliar. A name liketmpordf2might be a scratch data frame, or it might be the only copy of something you need. Never delete on name alone. - Find which objects your script actually uses. Search your script for each name. In RStudio you can use Find in Files across the project. If a name appears only once, at the point it was created, it is almost certainly dead weight.
- Remove one object at a time. Use
rm(object_name). For several at once, pass them as a character vector:rm("df1", "df2", "tmp"). The quotes matter when you pass names as strings; without quotes,rm()looks for objects with those names directly, which also works. - Remove a whole pattern of scratch objects. If your scratch objects share a prefix, list them first with
ls(pattern = "^tmp_"), check the output, then delete withrm(list = ls(pattern = "^tmp_")). Always run the listing step first so you can see exactly what will go. - Confirm the deletion. Run
ls()again. The removed names should be gone. If a name is still there, you probably deleted a different object or the name had a typo. - Reclaim the memory. Deleting an object frees it for R’s own use, but the operating system may not see the memory returned immediately. Calling
gc()forces garbage collection and reports how much memory is now free. This is optional but useful when you are watching memory closely. - Clear everything when a session is truly finished.
rm(list = ls())removes all objects in the global environment. Treat this as a last resort: it is irreversible and will wipe anything you have not saved. Save your script and any outputs first. - Make cleanup part of your script. At the end of a long script, add a short block that removes intermediate objects by name. This keeps the workspace tidy for whoever runs it next and prevents accidental reuse of stale data.
Common Mistakes
Deleting by pattern without checking. A pattern like "^d" can match more than you expect. Always run the listing version of the command first and read the output.
Forgetting that rm() takes names, not values. Writing rm(df) removes the object called df. Writing rm("df") also works because rm() accepts character vectors. But rm(list = df) fails unless df is itself a character vector of names.
Assuming deletion frees memory instantly. R’s garbage collector decides when to return memory. If you need it back now, call gc(). Even then, the operating system may hold on to the pages for a while.
Removing objects that functions still need. A function defined earlier may refer to a global object by name. Deleting that object does not break the function until it is called, and then you get a confusing “object not found” error. Check function bodies with body() before deleting anything a function might touch.
Clearing the workspace in the middle of debugging. rm(list = ls()) during a debugging session destroys the evidence you were inspecting. Save the workspace or the objects you care about first.
Tips
Name scratch objects with a consistent prefix such as tmp_ so they are easy to find and remove together. Keep a short comment at the top of each script listing the objects it expects to exist. Use saveRDS() to store anything you might need later before deleting it; a file on disk costs nothing in RAM. When working interactively, restart R rather than deleting dozens of objects by hand; a fresh session is the cleanest workspace you can get. If you use RStudio, the Environment pane shows object sizes, which makes large leftovers obvious at a glance.
Troubleshooting
“Object not found” after cleanup. You removed something a later step needed. Reload it from its source file or script, or restore a saved copy with readRDS(). If you saved the workspace image, you can restart and load it, though saved workspaces are best avoided for reproducibility.
Memory still looks full. Run gc() and read the report. If the reported free memory is large but your system monitor disagrees, the operating system is holding pages; this is normal and usually resolves under pressure. Large objects held inside lists or environments will not be freed by removing a single name if another reference remains.
A pattern deleted too much. There is no undo for rm(). Re-run the part of your script that created the objects, or restore from a saved file. This is why the listing-first habit matters.
rm() seems to do nothing. Check for typos and confirm you are in the right environment. ls() only shows the global environment by default; objects inside functions are local and disappear when the function returns.
Objects reappear after restarting. Your session is loading a saved workspace image. Disable that behaviour in your R startup settings so each session starts clean.
Frequently Asked Questions
Does rm() actually free memory? It removes the reference, which makes the object eligible for garbage collection. The memory is reclaimed when R next runs its collector, or when you call gc().
What is the difference between rm() and gc()? rm() deletes objects. gc() reports and triggers the cleanup of memory that is no longer referenced. You usually need both for a full cleanup.
Can I undo a deletion? No. rm() is permanent within the session. Save important objects with saveRDS() before deleting them.
How do I remove everything at once? rm(list = ls()) clears the global environment. It does not remove objects inside packages or functions, and it cannot be undone.
Should I save my workspace image? Generally no. Saving the image encourages sessions to carry stale objects forward. Save the script and the data you need instead, and start each session clean.
Why is my object still listed after rm()? Either the name was misspelled, the object lives in a different environment, or a package exports a function with the same name. Check with exists() and environment().