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

Solution 1:

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