The fastest way to delete a table in R

To delete a table in R, use the rm() function with the name of the table you want to remove. For example, if your table is called my_data, type rm(my_data) and press Enter. The table disappears from your R environment immediately.

If you want to delete multiple tables at once, list them separated by commas: rm(my_data, another_table, third_table). This works whether your tables are data frames, tibbles, or any other R object.

The rm() function only removes the table from your current R session — it does not delete the file on your computer. If you loaded the table from a CSV or Excel file, that file stays where it is. If you want to delete the actual file, you need to do that separately through your file manager or with a different R function.

Key Takeaways

  • Use rm(table_name) to delete a single table from your R environment.
  • Delete multiple tables at once by listing them with commas: rm(table1, table2, table3).
  • The rm() function removes tables from memory but does not delete files on your computer.
  • Clear your entire R environment with rm(list = ls()) if you want to start fresh.

Delete multiple tables with a pattern in their names

If you have many tables with similar names and want to delete only those that match a pattern, use rm() with the list argument and grep(). For example, if you have tables named data_2023, data_2024, and data_2025, and you want to delete only the 2023 and 2024 versions, type rm(list = grep("data_202[34]", ls(), value = TRUE)).

This command works by finding all objects in your environment that match the pattern you give it, then removing them. The grep() function searches for the pattern, ls() lists everything in your environment, and value = TRUE tells R to return the actual names instead of just the position numbers.

Be careful with pattern matching — test your pattern first by running just the grep() part without rm() to see which tables it finds. This prevents accidentally deleting something you meant to keep.

Clear your entire R environment at once

To delete every table and object in your current R session, use rm(list = ls()). This removes everything from memory and gives you a clean slate. It is useful when you are starting a new analysis or want to make sure no old data is interfering with your work.

After you run rm(list = ls()), your environment pane (in RStudio) will be empty. Any tables you had loaded are gone. If you need them again, you will have to reload them from your files.

This command does not affect your R scripts or the files on your computer — it only clears what is currently loaded in memory. Your code stays intact, and your data files remain where they are.

Delete a table and save your work

If you want to delete a table but keep your R script, just remove the line of code that creates or loads it, then save your script. The next time you run the script, that table will not be created.

If you have already run your script and created the table, delete it from memory with rm(table_name), then save your script file. The table is gone from your current session, but your script is preserved for future use.

In RStudio, you can also use the Environment pane on the right side of the screen. Click the broom icon to clear everything, or click the X next to a specific table name to delete just that one. This is the same as using rm() but does not require typing the command.

Undo a deletion if you made a mistake

R does not have an undo button for deleted tables. Once you run rm(), the table is gone from memory. However, if you loaded the table from a file, you can reload it by running the same command you used the first time — for example, my_data <- read.csv("my_file.csv").

If you created the table through calculations or transformations and did not save it to a file, you will need to recreate it by running the code that built it. This is why it is good practice to keep your R scripts so you can rerun them if needed.

To avoid losing work, save important tables to files before you delete them. Use write.csv(my_data, "my_file.csv") to save a table as a CSV file, or saveRDS(my_data, "my_file.rds") to save it in R's native format.

Delete tables from a specific package or library

If you loaded a table from a package (like a built-in dataset), deleting it from your environment does not uninstall the package. The table is still available in the package — you just removed your local copy.

To reload a package table after deleting it, use the package name with a colon. For example, if you deleted the mtcars table from the base R datasets, you can access it again by typing datasets::mtcars or by reloading the package.

If you want to permanently remove a package from your computer, that is a different process. Use remove.packages("package_name") to uninstall a package entirely, but this is separate from deleting individual tables.

Frequently Asked Questions

What is the difference between rm() and removing a file on my computer?

rm() deletes a table from your R session memory only. The file on your computer stays there. If you want to delete the actual file, use your file manager or the file.remove() function in R. For example, file.remove("my_file.csv") deletes the file itself.

Can I delete a table without typing its exact name?

Yes, use grep() with rm() to delete tables that match a pattern. For example, rm(list = grep("temp", ls(), value = TRUE)) deletes all tables with "temp" in their name. Always test the pattern first to make sure it finds only what you want to delete.

What happens if I delete a table that other code depends on?

If you delete a table and then try to run code that uses it, R will show an error saying the object was not found. You will need to recreate or reload the table before that code can run. This is why keeping your scripts is important — you can always rerun them to rebuild the table.

Is there a way to delete a table without using rm()?

In RStudio, you can click the X button next to a table name in the Environment pane to delete it. This does the same thing as rm() but does not require typing a command. Both methods remove the table from your current session.

Can I delete a table that is part of a larger dataset?

If you mean a column within a table, use my_data$column_name <- NULL to remove just that column. If you mean deleting an entire table that you created from another table, use rm() as normal. The original table stays intact unless you delete it too.