247

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Matt Peterson
  • 5,169
  • 4
  • 32
  • 34
  • 23
    How?? My VSCode never remembers previous sessions. It always opens with a brand new `Untitled-1` tab. It drives me nuts – Green Nov 20 '16 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 Mar 28 '17 at 10:50
  • 7
    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". – rbaleksandar Apr 26 '17 at 07: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 Apr 27 '17 at 08:17
  • I think I misunderstood your previous statement. Yes, you need to have an active directory at all time in order for the session restore to work. I thought that you had problems with having other files opened and restored that are outside that active directory. Sorry. – rbaleksandar Apr 27 '17 at 09:38
  • 1
    Be aware, [you can lose work with this feature](https://github.com/Microsoft/vscode/issues/42701), at least in v1.19.3. – gman Feb 01 '18 at 05:58
  • the shorted way is to go the directory of the application delete it completely and install the application again! – Abdulkarim Kanaan Mar 26 '20 at 11:40
  • for those who want to open new file in the previous session (in a new tab, when VScode is off): `"window.restoreWindows": "preserve",` https://github.com/microsoft/vscode/issues/36964 – JinSnow Feb 15 '22 at 09:28

11 Answers11

279

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.

ghlecl
  • 3,127
  • 1
  • 16
  • 15
  • 2
    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. – Matt Peterson Nov 11 '16 at 15:09
  • 3
    Folders? I don't use folders. How about just files? – Green Nov 20 '16 at 15:07
  • @Green You can have one active directory (workspace) but any number of opened files within or outside that workspace (at least as of the 26 of April 2017 when I discovered that this setting exists at all :D). – rbaleksandar Apr 26 '17 at 07:26
  • 2
    I don't have `window.reopenFolders` as a setting option. Instead I have `window.restoreWindows` – Ari Seyhun Sep 07 '17 at 07:58
  • I have an issue where VSCode opens the folder I _first ever opened_ with VSCode instead of the one I _most recently used_. I tried deleting VSCode's app data and it still defaults to the first folder I ever opened. Any idea how to fix this? – Patrick Michaelsen Nov 09 '17 at 16:46
  • @PatrickMichaelsen I do not, unfortunately. I would have tried something much like you tried: backup my settings, uninstall, search for all data folders (user, system) and reinstall. – ghlecl Nov 10 '17 at 18:05
  • 2
    A thing that might annoy some: VS Code will still restore the workspace if it contained a tab with an unsaved file. – Wells Mar 30 '18 at 21:48
  • 1
    Worst default "feature" I have ever seen. I lost a lot of productive work time wondering what was going on and why my text file was not being updated. – philologon Jul 03 '18 at 16:58
  • where to edit the settings or what is the name of the file? – anhtv13 Sep 12 '18 at 02:19
  • 4
    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 Mar 29 '19 at 20:27
  • Fixed my folder specific issues by doing `chmod 555 ~/.config/Code/User/workspaceStorage/` Not super happy about it but it works – gnomed Mar 29 '19 at 20:40
  • Switching off the Files.hotexit helped me. It was concerning when it was able to open a deleted file. – Paul McCarthy Oct 01 '19 at 15:03
  • where to set this option? – LumbusterTick Jan 02 '21 at 12:18
  • I really want this to work like Chrome tab groups. I can restore them later, but it doesn't happen by default. Why not both? – Apriori Mar 15 '22 at 22:27
  • Still if I open Code from cli it reopens the windows even with these settings – Артур Гудиев Apr 15 '22 at 06:23
  • None of the solutions work for me. As son as I close a file that is not save it keeps opening. VSCode becomes unusable – fdm Nov 24 '22 at 09:38
  • My vscode already has this set to none, and yet the open files are restored when I open the directory. – Karatekid430 Mar 10 '23 at 05:59
109

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

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
47

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

"window.restoreWindows": "none"
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Mathieu DOMER
  • 581
  • 4
  • 4
20

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.

Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 3
    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. – Álvaro González May 29 '18 at 08:45
  • where is the startup? I cannot find where to add the -n. Is it a file? Can I access it via Settings? – snowleopard Jan 14 '19 at 16:28
  • Only this worked for me on MacOs: "code -n ~/myFolder/myCodeFolder" – Denis Sep 10 '19 at 06:40
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

Sampath
  • 63,341
  • 64
  • 307
  • 441
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.

Wak Joko
  • 21
  • 1
  • 1
2

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.

David E
  • 21
  • 2
  • 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](/help/how-to-answer). – Community Mar 13 '23 at 19:14
1

Below worked for me

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

enter image description here

Vatsal Shah
  • 1,334
  • 18
  • 21
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.

Khaled Alam
  • 885
  • 7
  • 12
-2

For me the only solution that worked was to go to the solution root and delete the .vs folder.

  • By "solution", I assume you mean Visual Studio? Because the question was for Visual Studio Code. – Gino Mempin Oct 10 '21 at 13:50
  • I do not understand why the gave you -1. I know this is not a solution, but so far is the only thing that works for me, and only until I close a file without saving, then I get the same issue. The REAL solution I found not to get crazy annoying is not to use VS Code – fdm Nov 24 '22 at 10:01
-3

I reinstalled Visual Studio Code by downloading the latest update. I did not have to uninstall the previously installed Code. It work ok for me now.

  • Not even that works for me. I guess that those who gave you a -1 do not have any deadline coming soon and they can spend the enter day googling and playing with config files. Nothing in this post work form. But just to say that I do really appreciate the effort made by people to help others. – fdm Nov 24 '22 at 10:11