Restore a deleted file in the Visual Studio Code Recycle Bin
Asked Answered
S

30

196

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

Sella answered 21/12, 2016 at 15:3 Comment(6)
Since VS Code shows that hint inside the program I find this question very legit. I too looked for a minute inside of the program before googling (and landing here) and before looking in the system trash bin ;)Puklich
yes, this is a lacking featureDurable
I recommend checking out this underrated answer.Hotze
In addition to the recycle bin, check the own vscode backsup: Local History: Find Entry to RestoreOverripe
The previous comment is by far the easiest solution. It looks to have been added somewhat recently.Crucify
thanks @Janosh. This helped me a lot. Running find . -name "*.extension" -exec grep whatYouRememberFromFile {} + worked fantastically and showed me files in .config/Code/User/History I could restore. Thanks.Rahr
P
270

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 / ...

Puklich answered 29/12, 2016 at 15:42 Comment(6)
In Linux, you can restore using a terminal with the trash-cli package. trash-list will list all files in the trash, trash-restore foo let you guide you through restoring a file named foo. Useful in ChromeOS where there may be no GUI app with access to the Linux trash installed.Smalt
I arrived here because I accidentally rm'd an important file, trash-cli did the trick. You just litteraly saved me @RayBabarababassu
For me the debugger wreaked havoc and deleted the folder. Nothing in the Recicle Bin. The tabs disapeared too.Bilk
Found it in one of %APPDATA%\Code\User\History subfolders though.Bilk
@Bilk You just saved me a bunch of time. I accidentally deleted my whole src folder instead of one of the subfolders and was able to recover the files that weren't in version control yet.Seascape
But be aware that only saved files occur in the History folder, deleted + unsaved files don't.Puklich
G
64
  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.
Gigigigli answered 8/8, 2019 at 9:54 Comment(0)
F
49

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://static.mcmap.net/file/mcmap/ZG-AbGLDKwftXV-pWS2nZ7-ocVI0bRywWRfQcFyQcC2jaRA/media/microsoft/vscode-docs/vnext/release-notes/images/1_52/explorer-undo.gif

Ferrol answered 5/12, 2020 at 0:32 Comment(3)
This is especially relevant, as there's currently a bug in VSCode (or, more accurately, I think it's in Electron or some dependency) that causes VSCode to freeze when you trash a file, so some users have bypassed the "trash" file, and just straight up delete them.Crucify
Is there a way to disable that feature? Because now we can break the project because of an 'Undo' command executed incorrectly. Something can be undone inside the explorer panel and it can be confusing to understand what happened and why the project is broken, again, and again. We type cmd+z many times a day, you changed this feature that might lead us to undo something elsewhere we're not really looking at.Amoy
@Amoy I don't see an undo setting related to this feature. So that would be a good request to record as an issue.Ferrol
M
36

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.

Mallorymallow answered 10/12, 2021 at 12:4 Comment(3)
Don't know what empty space you mean... I've tried to click every single fragment, but it does not help. Nothing is happening... Damn....Rhabdomancy
@Rhabdomancy Click inside the file explorer of the VS code. It usually sits on the left side and you see files listed vertically. And then press undo. If you have restarted VS code in the meanwhile then it might not be possible anymore.Mallorymallow
@Mallorymallow it did not help. Probably it works for a separate file. In my case - I've deleted a whole folder...Rhabdomancy
M
26

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

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!

Midstream answered 1/8, 2023 at 21:5 Comment(1)
You can also F1 > "Local History: Find Entry to Restore" and search for the name of the deleted file. However, maybe because I was using remote SSH, when I selected the file, it simply showed it to me, instead of actually restoring it. I had to copy the contents.Maisiemaison
I
23

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!

Ileus answered 2/12, 2020 at 3:20 Comment(0)
B
20

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.

Bilk answered 21/6, 2022 at 20:31 Comment(3)
And if you're using remove development, ~/.vscode-server/data/User/History --> grep through the files thereModestine
@Modestine Your comment worked for me. You are a life saver. Please considering posting your comment so many other people can benefit from it.Musk
Spent ages trying to look it undelete options for WSL/ubuntu and few looked promising. However Spartan/iutlu is exactly what I needed. Life saved.Mingmingche
M
15

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.
Magic answered 31/8, 2019 at 23:33 Comment(2)
This did not work for me but made it worse ...Supercharge
You can also see the changes in the Timeline section. You can create a new file with the same file name, and the VS Code Timeline will show the content from before it was deleted.Spiritualize
B
10

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.

Bedchamber answered 24/5, 2019 at 12:5 Comment(0)
H
9

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"
Haller answered 12/11, 2019 at 20:35 Comment(0)
C
7

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.

Coypu answered 22/8, 2022 at 9:53 Comment(1)
Thanks a lot for this @MajorTom, in my case I lost the files from VSCode. In entries.json there's a reference to the original file, where every log is a snapshot with a timestamp. These tsx files contains the file state at given time. code "entries" :[ { "id": "S17j.tsx", "timestamp": 1663258186343 }, { "id": "A8Ql.tsx", "timestamp": 1663329533427 }, ] Smashed
H
6

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.

Hanukkah answered 10/5, 2022 at 15:33 Comment(1)
I tried that, It recovered the folder which the debugger deleted and the last written file in it, not more. The rest i found in %APPDATA%\Code\User\HistoryBilk
T
5

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

That answered 6/6, 2023 at 10:44 Comment(0)
T
4

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.

Torres answered 15/12, 2023 at 1:18 Comment(0)
T
3

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.

Tactual answered 14/3, 2019 at 14:41 Comment(0)
R
2

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

Rodarte answered 15/7, 2020 at 15:30 Comment(6)
Hi I am facing the same issue but in my case, there is no previous versionCalicle
It will not work if your File history is turned off and you won't be able to recover.Calicle
@SaadAbbasi I had tried for 2 days but this restoring the previous versions is the only solution I foundRodarte
Unfortunately, my file history was not enabled so I was not able to recover. I am going to do all stuff again. :(Calicle
Can I enable File history option without an external drive? I want to enable itCalicle
@SaadAbbasi Not sure of those things but here I have some resources which might help link, [link](wintips.org/fix-restore-previous-versions-not-working-in-windows-10/) this might work :-)Rodarte
T
2

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

Touching answered 9/6, 2021 at 18:15 Comment(1)
Good tip, i found that vscode has its own history folder at %APPDATA%\Code\User\History should the feature be disabled...Bilk
N
2

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

Nodababus answered 20/5, 2022 at 19:4 Comment(3)
Hard to right click on a file that is deleted ;)Bilk
You can create a new file with the same file name, and the VS Code Timeline will show the content from before it was deleted.Spiritualize
how can you do this on an entire folder?Anklebone
P
2

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.

Pich answered 9/7, 2022 at 15:19 Comment(0)
O
2

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

Onym answered 7/11, 2023 at 11:35 Comment(0)
O
1

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
Overabound answered 5/10, 2022 at 10:5 Comment(0)
S
1

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!

Supercharge answered 1/12, 2022 at 8:49 Comment(0)
S
1

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

Simeon answered 13/7, 2023 at 10:24 Comment(0)
T
1

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.

Tunic answered 23/3 at 10:5 Comment(0)
G
0

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
Griseofulvin answered 7/3, 2021 at 0:55 Comment(0)
P
0

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.

Protector answered 6/1, 2022 at 18:27 Comment(0)
U
0

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.

Unkenned answered 21/3, 2023 at 13:29 Comment(0)
D
0

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.

Dioptric answered 24/6, 2023 at 16:12 Comment(0)
A
-1

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.

Atelectasis answered 12/12, 2019 at 0:20 Comment(0)
R
-2

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.

Rashid answered 21/2, 2022 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.