18

I'm interested in the backup mechanics provided by VS Code. "Crash" includes both sudden power outages and handled exceptions. As examples of what I mean,

  1. Notepad++ has a backup folder which periodically saves copies of your files. Nothing is lost beyond the last 7 seconds even if the power goes out.
  2. Atom keeps an IndexedDB which should recover everything after an exception. I have no idea what happens on power outage.
  3. Inkscape relies on its exception handling to recover data. If the power goes out, the data is lost.
  4. Scite has no backup plan.
starball
  • 20,030
  • 7
  • 43
  • 238
Kevin Yin
  • 844
  • 1
  • 8
  • 26

3 Answers3

13

According to https://code.visualstudio.com/blogs/2016/11/30/hot-exit-in-insiders,

The way hot exit works is to periodically make backups of unsaved files. If VS Code happens to crash, a backup restore will occur the next time the folder is opened.

On Windows, the backup folder is C:\Users\<username>\AppData\Roaming\Code\Backups.

As for the period between backups, I haven't found anything.

PSEUDO
  • 113
  • 4
  • 13
Kevin Yin
  • 844
  • 1
  • 8
  • 26
  • 1
    note this is a questionable feature. My experience. Quit VSCode with unsaved file. Expect like **all** other apps I own unsaved changes are not saved. Git pull. Launch VSCode. VSCode silently loads backup not noticing that file was changed since VSCode exited. if I save (which I did) I miss the fact that I just saved an old version of the file that was updated when I did git pull. I turned off that feature of vscode – gman Apr 13 '19 at 15:18
  • 1
    Thanks, this saved me! – Izzy Rodriguez Aug 12 '21 at 23:53
4

Brief answer to the question "How does Visual Studio Code recover data after a crash?":

  • VSCode runs a "Hot exit". In my experience it's pretty reliable. When it runs, it creates a copy of all unsaved files that were open in the tabs of a VSCode window (they call the window an Editor).

  • VSCode saves these copies in a Backups directory. It also generates a list of paths to the Editors that contained unsaved files.

  • You can reopen a previously open Editor in VSCode and see your backed up files.

Steps for meeting typical needs

To list the Editors that were open when Hot exit ran

  • using Terminal on MacOS

    cat $HOME/Library/Application\ Support/Code/Backups/workspaces.json`  
    
    • alternative using jq for a more readable list

      cat ~/Library/Application\ Support/Code/Backups/workspaces.json | jq '.'
      
  • using Terminal on Linux

    cat $HOME/.config/Code/Backups/workspaces.json
    
  • using Powershell on Windows

    gc %APPDATA%\Code\Backups\workspaces.json
    

To browse the Backups directory

code $HOME/Library/Application\ Support/Code/Backups/

To Reopen the Editor you want, with the previously open files in the same tab order

code {{ folderUri from workspaces.json }}

Why these steps are useful

I often forget what directories I had opened before VSCode quits/crashes, and the menu to "open recent" from within VSCode doesn't indicate which Editors have unsaved files. For that need the workspaces.json list that VSCode generates seems irreplaceable.

VSCode docs:

Hot Exit
VS Code will remember unsaved changes to files when you exit by default. Hot exit is triggered when the application is closed via File > Exit (Code > Quit on macOS) or when the last window is closed.

Kay V
  • 3,738
  • 2
  • 20
  • 20
0

I found versions of recent edited files in C:\Users<username>\AppData\Roaming\Code\User\History