The fastest way to delete a table in R

To delete a table in R, use the rm() function with the name of your table. If your table is called my_data, type rm(my_data) and press Enter. The table disappears from your workspace when ready. This works whether your table is a data frame, a tibble, or any other table format R recognizes.

If you want to delete multiple tables at once, list them separated by commas: rm(my_data, my_other_data, results_table). R removes all three in one command.

To clear your entire workspace and delete every table and object you have created in your current session, type rm(list = ls()). This is useful when you are starting fresh or moving to a new analysis, but be careful — there is no undo button. Once you run this command, everything is gone unless you saved it to a file first.

Key Takeaways

  • Use rm(table_name) to delete a single table from your R workspace.
  • Separate multiple table names with commas to delete several tables in one command.
  • Use rm(list = ls()) to delete all tables and objects in your current session at once.
  • Deletion in R is permanent within your session — save your work to a file before clearing your workspace if you might need it later.
  • Deleting a table from your workspace does not delete the file it came from, only the copy R is holding in memory.

Why you might want to delete a table

As you work through an analysis, you often create temporary tables — filtered versions of your data, practice tables while you learn, or results you have already saved to a file. Keeping these in your workspace makes it harder to find the tables you actually need and uses memory your computer could use for other work.

Deleting tables you no longer need keeps your workspace clean and organized. This matters especially if you are working with large tables that take up significant memory, or if you are running a long script and want to free up space before the next step.

Deleting a table without using the command line

If you use RStudio, you can delete a table by clicking on it in the Environment pane (usually on the right side of your screen) and then clicking the broom icon at the top of that pane. This does the same thing as typing rm(), but some people find it easier to see what they are deleting before they confirm.

The Environment pane shows every table and object in your current workspace. Clicking the broom next to a single table deletes only that one. Clicking the broom at the top of the pane (with no table selected) clears your entire workspace, which is the same as running rm(list = ls()).

What happens when you delete a table

When you delete a table from R, you are removing it from your computer's memory — the temporary storage R uses while you work. The table no longer appears in your Environment pane, and you cannot reference it in commands anymore. If you try to use a deleted table in a calculation or plot, R returns an error saying the object was not found.

Deleting a table does not touch the original file on your computer. If you loaded the table from a CSV file, an Excel file, or a database, that file remains unchanged. You can reload the table anytime by running the same command you used to load it the first time. This is why it is safe to delete tables from your workspace — you can always bring them back if you need them again.

Deleting specific rows or columns instead of the whole table

Sometimes you do not want to delete the entire table, only certain rows or columns within it. Use square brackets with the table name to keep only the parts you want. For example, my_data <- my_data[, -2] removes the second column and saves the result back to the same table name, overwriting the original.

To remove rows instead, specify the row numbers before the comma: my_data <- my_data[-c(1, 5, 10), ] removes rows 1, 5, and 10. The minus sign tells R to keep everything except those rows. After you run the command, the table is updated in your workspace, and the deleted rows are gone from that point forward.

Preventing accidental deletion

R does not ask for confirmation before deleting a table, so a typo or a misclick can wipe out work you meant to keep. The safest habit is to save your tables to files before you delete them from your workspace. Use write.csv(my_data, "my_data.csv") to save a table as a CSV file, or saveRDS(my_data, "my_data.rds") to save it in R's native format.

If you are working on a long script, comment out the deletion commands while you test. Put a # at the start of the line: # rm(my_data). This way the command does not run, but you can uncomment it later once you are sure you no longer need the table. Many people also keep a separate script file with all their deletion commands, so they can review what they are about to clear before running them all at once.

Frequently Asked Questions

Can I undo a deletion in R?

No. Once you delete a table with rm(), it is gone from your workspace for that session. If you saved the table to a file before deleting it, you can reload it. If you did not save it, the only way to get it back is to re-run the code that created it in the first place.

What is the difference between deleting a table and clearing my workspace?

rm(table_name) deletes one specific table. rm(list = ls()) deletes everything in your workspace at once — all tables, all variables, all objects. Use the first when you want to clean up a few things. Use the second when you are starting a fresh analysis or closing out a project.

Does deleting a table in R delete the file it came from?

No. Deleting a table from your R workspace only removes the copy R is holding in memory. The original file on your computer stays exactly as it was. You can reload the table anytime by running the command that imports it.

What if I delete a table by mistake?

If you saved the table to a file before deleting it, reload it using the same import command you used originally. If you did not save it, you will need to re-run the code that created it. This is why saving your work to files is important — it gives you a way to recover from accidents.