289

Visual Studio Code always seems to remember my session and reopen the files and/or projects that were open the last time I used it. It obviously behaves correctly when running it from the command line with a file or folder supplied, but when opening from a taskbar shortcut, I'd like it to default to an empty environment.

Is there any way to change this behavior?

8
  • 25
    How?? My VSCode never remembers previous sessions. It always opens with a brand new Untitled-1 tab. It drives me nuts
    – Green
    Commented Nov 20, 2016 at 15:06
  • 3
    @Green Currently it only remembers previous files if you have a folder/workspace opened. So just open any random folder (i chose my desktop folder) and now it remembers any file between sessions, even those not from that folder.
    – Cardin
    Commented Mar 28, 2017 at 10:50
  • 8
    A big fat +1 from me because this question actually revealed to be that VS code CAN RESTORE previous session. XD Also in terms of what @Cardin states things have changed. I have the latest VS code running on Win10 and setting "window.reopenFolders" : "all" opens every single file no matter if it is in the currently active directory or not. Sadly it seems that VS code still doesn't support having multiple active directories because of all that "active directory = workspace". Commented Apr 26, 2017 at 7:21
  • @rbaleksandar That's still a negative. VS Code does not reopen existing non-dirty files as of ver 1.11.2, even with "window.reopenFolders" : "all". To test, 1) go to File>Close Folder, 2) drag and drop any random file into VS Code, 3) go to File>Exit, 4) Relaunch VS Code, 5) Notice you get a blank VS Code editor again. In order to restore sessions, you need to have an active directory open. Any session, regardless of file location, will be remembered as long as you have an active directory. Btw, you can install Project Manager extension to jump between multiple active directories.
    – Cardin
    Commented Apr 27, 2017 at 8:17
  • 1
    Be aware, you can lose work with this feature, at least in v1.19.3.
    – gman
    Commented Feb 1, 2018 at 5:58

11 Answers 11

316

You can also go into your settings and use the following:

"window.reopenFolders": "none"

which will not reopen the folders you were working on when you closed the editor. The other options are one (the default) and all.

Edit 2017-11-09:

The option is now changed in latest versions.

"window.restoreWindows": "none"

See Mathieu DOMER's answer.

Edit 2018-09-12:

Another setting related to this is the hotExit setting. This has been discussed in this answer to a related question. To prevent reopening and remembering unsaved files, you can set this to:

"files.hotExit": "off"

But from the test I've made, when the window.restoreWindows setting is set to none, this is not needed. I haven't tested every possible combination, so YMMV.

And to answer a question in the comments, to edit the settings, you have to open the settings file. Some documentation can be found here (at least on the date I am writing this).

Edit 2022-03-16:

If you prefer using a GUI to change the settings, see D'Arcy Rittich's answer.

18
  • 3
    I'm changing the accepted answer to this one, as I feel like the settings approach is better than a command line switch, which only affects the one shortcut. Commented Nov 11, 2016 at 15:09
  • 3
    Folders? I don't use folders. How about just files?
    – Green
    Commented Nov 20, 2016 at 15:07
  • 2
    I don't have window.reopenFolders as a setting option. Instead I have window.restoreWindows
    – Ari Seyhun
    Commented Sep 7, 2017 at 7:58
  • 2
    A thing that might annoy some: VS Code will still restore the workspace if it contained a tab with an unsaved file.
    – Wells
    Commented Mar 30, 2018 at 21:48
  • 6
    This isn't working for me when I open a folder with code . command. Always restores open editors and I can't stand it
    – gnomed
    Commented Mar 29, 2019 at 20:27
134

In VS Code:

  • for Windows/Linux Ctrl+, (or choose File/Preferences/Settings) to open the settings page.
  • for Mac +, (or choose Code -> Preferences -> Settings) to open the settings page.

then type restoreWindows in the Search settings input to filter for this setting. Set it to none and restart VS Code.

restoreWindows

0
49

With latest update, it seems that the parameter has changed, now use:

"window.restoreWindows": "none"
0
22

You can add the -n option to the startup of VS Code and it will always start with an empty window, not restoring your previous session.

3
  • 6
    The option is definitively there (code --help displays -n, --new-window Force to open a new window.) but it still reopens all previous windows. I think it's only meant to do something meaningful when the program is already running. Commented May 29, 2018 at 8:45
  • where is the startup? I cannot find where to add the -n. Is it a file? Can I access it via Settings? Commented Jan 14, 2019 at 16:28
  • Only this worked for me on MacOs: "code -n ~/myFolder/myCodeFolder"
    – Denis
    Commented Sep 10, 2019 at 6:40
6

Go to File -> Preferences -> Settings

In the "Search Settings" bar type Restore Windows. You will see Window:Restore Windows. Set it to none.

Or...

On the side menu, User should be underlined and you should see options like:

  • Text Editor
  • Workbench
  • Window
  • Features
  • Applications
  • Security
  • Extensions

Click on Window and scroll down on the main page until you see Restore Windows. Put the setting to none.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 13, 2023 at 19:14
2

01 December 2018

This works for me. i.e. "C:\Users\Sampath\AppData\Local\Programs\Microsoft VS Code\Code.exe" -n

enter image description here

2

For me, none of above is working while I'm trying to close "dirty" unsaved files which I accidentally edited 1000 files and wanted to ignore saving all of them.

My fix was adding this line into settings.json:

    "files.hotExit": "off"

Open up vscode, close vscode and just click the confirmation button to close all of the files without saving.

Then open back vscode and boom.. no more unsaved files being shown.

2
0

If "window.restoreWindows": "none" not solve the problem,
then try to run code as root -> sudo code --user-data-dir code files and restart code normally without root.

0

Below worked for me

Right-click on Shortcut and add --disable-gpu to Target as per screen shot.

enter image description here

1
  • This works for me in both regular vs code and vs code insiders
    – Rose G
    Commented Jun 20, 2021 at 11:28
0

To prevent opening any previously opened workspace when starting VS Code without a resource (file/directory/code-workspace) argument, put "window.restoreWindows": "none", in settings.json.

To handle editor tabs for untitled (new, unsaved) files, put "files.hotExit": "off", in settings.json. See also How can I get VS Code to not remember unsaved changes when I exit it?.

From my basic testing, the two above measures don't prevent VS Code from recalling open editors and editor groups when reopening a previously opened workspace. There's at least one trick that I know of to work around this. You can put the following task in a workspace tasks.json file to make VS Code close all editor groups for that workspace on startup, or put it in your user-level tasks (run Tasks: Open User Tasks) file to make it run on open for all workspaces:

{
   "label": "close editor groups",
   "command": "${command:workbench.action.closeAllGroups}",
   "runOptions": {
      "runOn": "folderOpen",
   },
   "presentation": {
      "echo": false,
      "reveal": "never",
      "focus": false,
      "panel": "shared",
      "showReuseMessage": false,
      "clear": false,
   },
},

If you want to run that command manually instead of automatically, then just run View: Close All Editor Groups in the command palette.

For other stuff, see settings like terminal.integrated.hideOnStartup, workbench.editor.restoreViewState, or related commands that you can do something similar with the above folderOpen task.

There are feature requests about this:

-1

To stop it remembering open tabs (MacOS):

$ rm ~/Library/Application Support/Code/User/workspaceStorage/*
# chown root ~/Library/Application Support/Code/User/workspaceStorage
2
  • Doesn't delete other things aside from just the previously opened tabs/files? Commented Dec 25, 2023 at 2:48
  • @GinoMempin I have not noticed any issues and I have used this for some time Commented Dec 25, 2023 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.