Using Visual Studio Code Version 1.8.1 how do I restore a deleted file in the recycle bin?
Restore a deleted file in the Visual Studio Code Recycle Bin
374.4k views Asked by user2584621 AtThere are 30 answers
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:
- How do I find and restore a deleted file in a Git repository?
- git recover deleted file where no commit was made after the delete
- Recover from losing uncommitted changes by "git reset --hard"
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.
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!
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.
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
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.
Note: Manipulations in the drive after restore point won't be available
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
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,
- I discarded the changes in git, that the file was deleted.
- And THEN I retried ctrl+Z and boom all the changes previously came back magically!
I was a happy camper!
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
This worked for me!
- Try to recreate files (with extensions - files should be empty) and directory structure.
- There should be a
TIMELINE
on the left side of the bottom VSCEXPLORER
view (default setting).
- Right click on the last
File Saved
, then chooseRestore Contents
.
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!
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.
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.
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"
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.
Vscode, stores a copy your recently edited file in local history, I created a simple package that will help you find lost files.
Usage
- Install:
pip install vscode_deleted_file_recovery
- 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
- 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.
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.
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 / ...