Restore a deleted file in the Visual Studio Code Recycle Bin

374.4k views Asked by At

Using Visual Studio Code Version 1.8.1 how do I restore a deleted file in the recycle bin?

30

There are 30 answers

7
Manuel Manhart On

It uses the normal trash bin of your system. So you can grab it our of there.

In Windows you find it in the explorer, in Linux it is as well in Konquerer / Nemo / ...

0
Lucas De Lavra Pinto On

Like two other comments said

  1. You can press F1 inside VS CODE
  2. Write your file name
  3. Select data version of file

done!

enter image description here

2
starball On

It's not quite clear what "recycle bin" means here since I don't understand why it would be difficult to restore something from there (as in- your system's trash/recycling bin/folder/directory or whatever it's called. VS Code itself doesn't have an analogous feature built-in (though it does have something you might find even better)).

When deleting files from VS Code's Explorer View, they do go to the system's trash folder by default since version 0.5.0 (see also the files.enableTrash setting. Though I think on some systems there's an exception for deleting directories recursively, such as on Linux), and most systems' default file explorer applications will provide an easy way to restore the file to its original location from the system's trash folder.

But since this Q&A has been interpreted quite freely by the community and become what looks like a canonical, I'll try to give the best canonical answer I can.

VS Code Explorer Undo

As stated, if the last modification you made in the Explorer View was to delete something, since version 1.52 of VS Code, you can just focus the Explorer View and press the "Undo" keyboard shortcut (ctrl/cmd+z) and the deletion will be undone. See also the explorer.enableUndo and explorer.confirmUndo settings. This works for files, but I don't think it works for directories.

See also the explorer.confirmDelete setting.

Version Control Software

If you're using some form of version control such as Git, and the last version of the file has been saved in some way (Ex. Git stage or commit), and the internals of the VCS are still intact (you haven't deleted them too), then you can just use your VCS's mechanisms for pulling that saved copy into the working directory.

For example for Git, see the following:

Or if you've pushed the changes you want to some other remote, you can fetch them back from there.

VS Code Local History (for files with no unsaved changes)

Since version 1.66, VS Code also has a feature to keep a history of files you were working on in it: Local History. It saves a copy every time you save the file (well, not exactly. There are settings to control its behaviour that you can find in the Settings UI). The release notes list the commands that the feature supports, and the settings you can use to configure it. But what you probably want at this moment is the command Local History: Find Entry to Restore (run in the command palette), which opens a menu listing all the history entries, with a searchbar. The ID for that command is workbench.action.localHistory.restoreViaPicker.

I think you could also create a new file with the same name and path in place of the one you deleted, and view the Local History entries also in the Timeline View (a subview of the Explorer View), which you can focus using the Explorer: Focus on Timeline View command in the command palette.

If you find some reason to want to dig around, the Local History feature saves the history entries in files under the User/History subdirectory of the user data directory (you can specify a user data directory via commandline using the --user-data-dir commandline argument, but the default one is in a OS-dependent location, which is also where your user settings.json file goes under):

  • Windows %APPDATA%\Code
  • macOS $HOME/Library/Application\ Support/Code
  • Linux $HOME/.config/Code
  • For VS Code Server, the default user data directory should be ~/.vscode-server/data

Just be warned that the file names are not human-readable, so bring a commandline search tool with you like grep or something.

Some interesting notes from my brief experimentation with the feature: It seems like if VS Code is open with a workspace folder containing file A while you modify file A from some other program and write that modification to disk, it will still be recorded by VS Code. Also, the workbench.localHistory.exclude setting (which is empty by default) does not inherit from the files.exclude setting as is the case for other similar features like search.exclude. So if you manually edit files under ./.git/, even they will have local history entries! But (approximately) I don't think files that have ever been moved around or manually saved will have a history entry even if they are changed outside of VS Code until one of those things happens.

If you're interested, the full logic for whether something should be tracked by Local History is implemented in src/vs/workbench/services/workingCopy/common/workingCopyHistoryTracker.ts.

If you're using a version of VS Code prior to 1.66, you could use an extension. Try searching "local history" in the VS Code marketplace or Extensions View and take your pick. Of course, nothing will be saved by an extension until that extension has been installed on your machine, so if you didn't install them before losing your files, this won't help you with your immediate problem.

VS Code Hot Exit (for files with unsaved changes)

VS Code also has a feature called "Hot Exit" that remembers files that you didn't save before closing the VS Code window.

If you want to dig around, the storage for this feature goes under the "Backups" directory in the user data directory.

If you can't do any of that...

I think you're left with looking into data recovery. Ex.

1
Yuanjing Cai On

Running on Ubuntu 18.04, with VS code 1.51.0

My deleted files from VS Code are located at:

~/.local/share/Trash/files

Every deleted file have a corresponding .trashinfo file which contains details about where the file is deleted from and deletion date and these are located at:

~/.local/share/Trash/info

More info here.

To search for your deleted files:

find ~/.local/share/Trash/files -name your_file_name 

In case you deleted files form a mounted ntfs filesystem, they will be located at:

/path_to_mounted_fs/.Trash-$UID

You can get $UID by doing echo $UID in your terminal.

Hope my case helped!

7
VonC On

If you just deleted the file, know that VSCode 1.52 (Dec. 2020) will support:

Undo file operations in Explorer

Explorer now supports Undo and Redo for all file operations: delete, rename, copy, move, new file and new folder.

Make sure the focus is in the Explorer and trigger the Undo or Redo commands and your last file operation will be undone or redone respectively.
Keep in mind that we have separate undo stacks for the editor and the explorer and we choose which one to undo based on focus.

Explorer Undo -- https://media.githubusercontent.com/media/microsoft/vscode-docs/vnext/release-notes/images/1_52/explorer-undo.gif

1
Henry Obiaraije On

I tried most of the options above but none worked.

What worked for me was clicking and highlighting the folder where the file was deleted from in vscode. Then while it is selected, hit CTRL + Z. This works on windows.

Hitting CTRL+Z without highlighting that folder didn't work.

6
NIRANJAN S PATTANSHETTI On

While pushing a repository to Github through Vs Studio code I deleted whole folder and they were not available in Recycle bin also. Here is how I recovered those files. For Windows.

The method is to restore the previous version of the Drive in which the deleted file existed

I had deleted files from G: drive, the below images are self explanatory

Open properties menu of the drive

enter image description here

In properties go to previous versions tab, where you can find the previously stored versions of that drive along with date at time of backup use open or click on restore to get the previous version of that drive.

enter image description here

Note: Manipulations in the drive after restore point won't be available

3
Ashish Gautam On

A month recovery can be done in visual studio code 1 Right click on file and 2 click Open Timeline and 3 select file from timeline 4 the last time file saved by you 5 and your last change of the file is recovered

NOTE : Not only last but all the previously saved check points can be recoverd from vs code

0
mahatmanich On

I had the case that I, accidentally, deleted a committed file (git) with a ton of additional uncommitted changes, and I could not get it back. (two days of work! yeah, I know, commit early commit often, I know ...)

I had no linux trash can setup on my docker instance and was developing via remote ssh.

I tried to recreate the file and then do a ctrl+Z to recapture the changes as some suggested here. This did not work for me.

So after a struggle over an hour I finally gave up and thought the changes to the file were lost.

However,

  1. I discarded the changes in git, that the file was deleted.
  2. And THEN I retried ctrl+Z and boom all the changes previously came back magically!

I was a happy camper!

0
coltdorsey On

If your local directory has git initialized and you have not committed the changes that include the delete, you can use git checkout -f to throw away local changes.

0
Rodney Salcedo On

Yes, I know this question is about VS Code but I think I can help someone. I permanently deleted a file, and I tried all the tips wrote here with no success. So I had to recover the file from OS Linux following this

grep -a -C 500 -F 'Unique string in text file' /dev/sda
1
Chirag Taneja On

I am not sure how I deleted a file in VS Code, and couldn't find it in recycle bin. Found the file using the History feature of File Explorer.

File Explorer Options

File Explorer File History

File History is on

0
Balki On

If the deleted folder from vscode on a macbook is present in One Drive (Which is a common thing for software professionals) the deleted folder will not be in Recycle bin and rather in One Drive home folder.

0
Gurukota On

EASY FIX

If you know the name of the deleted file then recreate it with the exact name and filepath. Open the file, then go to TIMELINE as shown in the image below, you'll find all the changes you made to the file.

IMAGE

7
rakso On

This worked for me!

  1. Try to recreate files (with extensions - files should be empty) and directory structure.
  2. There should be a TIMELINE on the left side of the bottom VSC EXPLORER view (default setting).
    VSC timeline view
  3. Right click on the last File Saved, then choose Restore Contents. VSC timeline view - with opened context menu

Hope it saves someone like it saved me 3 hours of life!

If not, I recommend trying the way described in this answer (by @iutlu [remote development] and @Spartan) - it was helpful to me, only that there were a lot of versions of each file - but I'm not saying no: the files were!

2
flash On

I know the OP says Recycle Bin. What I do though is recreate the file, especially if it's a single file. And when in the file, I just press CMD+Z (I'm on a Mac) and I get my file back.

  1. Recreate the file in the same directory from where it was deleted.
  2. CMD+Z inside of the newly created file.
0
John On

Just look up the files you deleted, inside Recycle Bin. Right click on it and do restore as you do normally with other deleted files. It is similar as you do normally because VS code also uses normal trash of your system.

7
lockhrt On

Click in empty space of VS Code's explorer (it's where you see files listed vertically) and press undo Ctrl + z

It'll recover your permanently deleted files too.

VS Code is a lifesaver. ✌

0
Mark On

It was asked above if there is a way to disable the ability to delete a file you created with an undo (@rochasdv).

There is a new setting to disable undo for file operations:

Explorer: Enable Undo default is warn, confirmation dialog

You can also set it to disable so that your files/folders will not participate in undo operations.

You can alo set it to allow - the pre-new setting situation.

Currently, this new setting is in the Insiders Build v1.64 so it may be in Stable early February, 2022.

1
karlpothast On

VS Code saves any file you actually edit - not all files in a project directory however - to a local directory :

Linux : $HOME/.config/Code/User/History
Mac : ~/Library/Application Support/Code/History
Windows : %APPDATA%\Code\User\History\

There are directories created for every file you edit with different versions tied to a timestamp/session defined in a JSON file called entries.json like @MajorTom mentioned so your work is saved but it is a tad bit cumbersome to restore a directory like you would restoring from the trash.

If you simply want to restore the latest version of a directory you accidentally deleted, you can run this .net console utility and it will restore everything it possibly can to a directory you specify in the appsettings.json.

https://github.com/karlpothast/VSCodeRecoveryTool

1
Muhammad On

who still facing the problem on linux and didnt find it on trash try this solution

https://github.com/Microsoft/vscode/issues/32078#issuecomment-434393058

find / -name "delete_file_name"
3
Spartan On

If you can't find your files in the Windows Recycle Bin as it happended to me
(debugger went rogue and deleted the project folder)
look in the %APPDATA%\Code\User\History\ subfolders sorted by date modified. There I could retrieve my seemingly lost files.

0
CerisePelican On

I am working on Windows with a Ubuntu WSL. I permanently deleted a file by mistake and was not able to restore it from the recycle bin nor by doing anything else.

I however, was able to get my files content by going to "Timeline" and see all my previous modification (it will open a comparison window).

Hope it will help somebody.

0
NITHESH On

If you completely delete any of the components in angular using visual code, you can restore it Go to Recycle Bin and restore the deleted component that will be restored in the project.

0
Chetan Jain On

Vscode, stores a copy your recently edited file in local history, I created a simple package that will help you find lost files.

Usage

  1. Install:
pip install vscode_deleted_file_recovery
  1. Create a Python file anywhere on your PC and run the following code to search for files:
from vscode_deleted_file_recovery import Recover
Recover.print_files(search_term="/Chetan/Documents/tasks")  # Replace with your desired search path
  1. Run the following code to restore deleted files. A folder named _recovery will be created in the same directory where you run it:
from vscode_deleted_file_recovery import Recover
Recover.restore_files(search_term="/Chetan/Documents/tasks")  # Replace with your desired search path

You can find restored files in the Recovery folder where you ran the command.

0
mallet On

For Mac, try to search some parts of the code in the deleted file in the following path:

~/Library/Application Support/Code

I found a deleted file in this folder

~/Library/Application Support/Code/Backups
1
MajorTom On

If you have permanently deleted files, on macOS, you can see the history (the last month I guess) in $HOME/Library/Application Support/Code/User/History/. You will have to find your file(s) by looking at each entries.json file in every subfolder and rename the last version of your file.

There is probably a similar way on windows and linux but I don't know the precise paths.

0
Kudret Batuhan On

If you want to find the deleted files, after running

Run

in order, you can see the changes you have made before using

APPDATA%\Code\User\History\

path. By the way, you have the opportunity to examine the files according to the recording dates.

1
Ravi Mengar On
  1. First go to Recycle Bin of your local machine.
  2. Your VS code deleted files is there in Recycle Bin.
  3. So, Right click on deleted files and select-> Restore option then your deleted files will be automatically restored in your VS code.
0
Eugene Ihnatsyeu On

I accidentally discarded changes in the Source Control in VS Code, I just needed to reopen this file and press Ctrl-Z few times, glad that VS Code saves your changes like that.