When you exit RStudio, you’ll see a pop-up asking, “Save workspace image to ~/.RData?” If you’re unsure, you’ll probably select Save. After all, it is the default. Plus, saving is good. Right?
Well…it depends. The next time you load RStudio, everything in your workspace (global environment) will be exactly how you left it. If it took a really long time to load your data into R, then this is fantastic. You don’t have to go through that cumbersome process again! Phew!
But what if, instead, you had a bunch of useless crap you’d prefer never to see again? You might think, Ok, I’ll just clear my workspace and carry on. Only this time, at the end of my R session, I’ll click Don’t Save. No biggie!
The next time you load RStudio, that pesky workspace that you thought you deleted will be back in all its former glory. You try again—clear, quit, Don’t Save, load. Wait, what??? This time— rm(list = ls()), quit, Don’t Save, load. But…why?!
By saving your workspace, R saves your global environment as .RData files. Every time you start R, these files are used to restore your workspace. Deleting your working environment does not actually delete the .RData files—they’re still there. This is why you can clear your workspace, thinking it’s gone for good, only to find it pop back up again the next time you launch R.
Clearing the workspace you just can’t shake
If you find yourself having this issue, don’t worry! There is a solution. In fact, there’s more than one! The quick-and-dirty option is to clear your workspace, quit, and then choose Save. This time, R will be saving an empty workspace which will reload the next time you restart R.
The second option is to run: unlink(“.RData”) in the console. This won’t actually delete your environment, but it will delete the saved .RData files. Without the saved files, it won’t have anything to load upon startup. Just make sure not to click Save again or else you’ll just be writing new .RData files that you’ll have to delete again.
Adjusting your settings
If you don’t see yourself needing to save your workspace anytime soon, you might want R to quit asking about it. This can be changed through the General tab of the Global Options menu. To navigate to the Global Options menu, select Tools from the menu bar of RStudio (found at the very top). Global Options can be found at the very bottom of the Tools menu.
Once you’ve clicked Global Options, an Options box should appear. Under the General tab, you should see a Workspace section. If you have .RData files that you’d like to keep, but you don’t want them loaded every time you start R, you can uncheck the box next to “Restore .RData into workspace at startup.” Alternatively, if you don’t want your workspace saved when you close RStudio, select Never from the drop-down menu next to “Save workspace to .RData on exit.” Now that R knows your preference, it’ll quit asking you if you’d like to save!
With these solutions, hopefully your saved workspace will be gone for good. See you Never, workspace! 👋