Score:1

How to recover a deleted Ubuntu installation in Windows Subsystem for Linux

fo flag

I had both Ubuntu 18.04 and Ubuntu20.04 and wanted to delete Ubunto18.04. However, I deleted Ubuntu20.04 mistake. Is there a way for me to restore the files from Ubuntu 20.04?

Nandete82 avatar
kr flag
Hi, it's unlikely... it depends on how you had the different systems and partitions. Did you have 3 different systems in one machine? Also, how have you removed it? Please read [How to ask](https://askubuntu.com/help/how-to-ask)
NotTheDr01ds avatar
vn flag
@Robert-Smolders A heads-up that this is more a Windows question than an Ubuntu one. WSL questions *are* on-topic here when they are about Ubuntu, but in the future consider [Super User](https://superuser.com) for anything specific to the WSL/Windows functionality.
NotTheDr01ds avatar
vn flag
That said, I'd personally recommend against downvotes or votes to close (although it remains, of course, each individual's choice) -- This is (a) definitely not a duplicate of "deleted partition" questions, and (b) It is on-topic as a WSL/Ubuntu question.
Robert Smolders avatar
fo flag
@Nandete82 Yes, I had 3 different systems on one machine. My laptop has windows 10 and I had installed Ubuntu 18.04 and Ubuntu 20.04 from the Microsoft store. I deleted my Ubuntu 20.04 by searching for it in the windows search bar and then clicking on delete.
Robert Smolders avatar
fo flag
@NotTheDr01ds Thank you for the information
Score:1
vn flag

Possibly, but with caveats. If WSL2, then probably not (but you can try). If WSL1, then you may be able to recover some individual files.

First, was there difficult-to-recreate data in the WSL instance, or is your concern just the time-lost in rebuilding the image? I can tell you now that file-recovery is going to be time consuming, so if you don't have important files that were lost, just plan on installing a new Ubuntu WSL instance and reconfiguring it.

If you do need to attempt to recover critical data, then shut down Windows immediately and read the rest of this on your phone or tablet.

It's important to understand that the removal of a WSL instance is a "true" delete of the files involved -- nothing goes to the Recycle Bin (I've tested to be sure).

If you were using WSL1, then the filesystem was stored directly on the NTFS drive, and recovery for certain files might be possible. It's my understanding that this tends to work better on magnetic disks, but some utilities claim to be able to handle solid-state drives as well. Let's put it this way -- I've only had to try it on a magnetic drive.

Regardless, it's unlikely that you'll be able to get the whole image back -- The goal here is simply to recover any difficult-to-recreate files that you may have stored there (e.g. in $HOME).

You'll need a utility specifically designed to recover deleted files. One suggestion is found in this answer, but I have no experience with it. You'll also find a number of suggestions in other Super User threads.

You should consider creating a separate bootable USB (from a separate computer). Windows is constantly writing to the drive when in use, and any file writes could be in locations where your deleted data is stored.

If possible (and it's been a while since I've done this), you'll want to look for files that were deleted from C:\Users\youruser\AppData\Local\Packages\Canonical...\LocalState\. Again, for WSL1, each individual file will show up in the filesystem.

For WSL2, it's just one virtual disk named ext4.vhdx. If you can undelete it, then it may still have damage. If you get extremely lucky and can recover it without errors (and you won't know until you try) then you can use it by simply:

  • Reinstalling Ubuntu 20.04 from the Microsoft Store
  • Copying the recovered ext4.vhdx file back over the newly created one.

But it's more likely that the file will be corrupted to some degree. If that's the case, then you might still be able to recover files from it by repairing the vhdx. That's simply beyond my experience, sadly (or not!). From a quick search, it looks like most options require a purchase of a software license, and even then they look shady to me (e.g. purporting to call themselves a "Microsoft" product when they clearly aren't). And after repairing the image, it may still not boot as a WSL disk, in which case you'd need to either:

  • Install Hyper-V (included with Windows Pro or higher), create a Linux VM, and mount the repaired vhdx there to see what files might be recovered.
  • Install Windows 10 or Windows 11 Preview, which has an updated WSL with the ability to mount vhdx files (in my understanding).
Soren A avatar
mx flag
Where does OP talk about WS ?. As he monitions partitions questions in the question it is probably a real Ubuntu installation.
NotTheDr01ds avatar
vn flag
@SorenA Well, first, the OP tagged it *windows-subsystem-for-linux*, and second, the original question title (before someone who didn't know any better edited it) specifically mentioned removing it from Windows. It was the *edit* that changed it to mention "partitions".
Soren A avatar
mx flag
I missed that the header and question was edited, But in that case, the question is about recovering files and functions in WSL and Windows and not about Ubuntu.
NotTheDr01ds avatar
vn flag
@SorenA Agreed, but it's been determined multiple times that WSL is on-topic here when it comes to Ubuntu distributions. As I pointed out in a comment to the OP, [Super User](https://superuser.com) would have been a *better* option. If it's clearly off-topic, I quite often refuse to answer questions on Stack sites and ask them to repost on the proper site. But we also can't expect a user to *know* that there's not an Ubuntu-based solution to a problem they experience on WSL. In this case, there isn't (that I'm aware of), but often there is Ubuntu specific knowledge that comes into play.
Robert Smolders avatar
fo flag
@NotTheDr01ds thank you for your answer! Most files that were deleted are simply time-consuming to create again. Unfortunately, I am using WSL2. However, I had Docker installed on my windows and connected it with my WSL2, so I did find two files named ext4.vhdx at C:\...\AppData\Local\Docker\wsl\data and at C:\...\AppData\Local\Docker\wsl\distro which are from 18-08 so maybe they still contain some info. After installing Ubuntu 20.04 again I tried to replace them, but Ubuntu won't startup. So, that probably doesn't work. I still want to thank you very much for the answers and the help!!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.