Score:3

I reset Ubuntu on WSL with "Setting > Apps > Ubuntu > Reset". Now all my files in WSL filesystem is lost. How can I recover it?

in flag

I am using Ubuntu on WSL2 on Windows (OS Version Details). I reset the Ubuntu app by giving:

Settings > Apps > Apps & Features > Ubuntu > Reset

Under 'uninstall' it's mentioned that uninstalling the WSL distribution doesn't affect the files, so does 'Resetting' a WSL distribution really remove the files in the WSL filesystem? I expected the /home directory files to get preserved but after reset I find that there is nothing left in /home.

Is it possible to recover the data that was there in /home?

Thanks for your time with this question.

Score:7
vn flag

Sadly, I don't have great news for you; only perhaps a suggestion when reinstalling that will hopefully prevent it from happening again.

First up, the Reset option in the screenshot you posted does say specifically:

If this app isn't working right, reset it. The app's data will be deleted.

So yes, a Reset did get rid of the entire Ubuntu WSL data (the filesystem and registry data).

A Repair, on the other hand, keeps the WSL Ubuntu distribution intact.

Important note: These options, and the wording behind them in the settings, are the same for all Store apps. I know that the Uninstall wording says:

Your documents will not be affected.

However, the way WSL installations are handled, an uninstall does remove the entire instance. The documents are part of the filesystem, which is removed on an uninstall.

I've seen cases where the Store apps are being reset for other reasons, resulting in WSL data loss. For that reason (and more), I recommend the following ...

Preventing Uninstall and Reset from removing WSL data

I'm seriously thinking about doing my first YouTube video on this soon. When installing Ubuntu (or any other distribution) in WSL, the first thing I do is "disconnect" the instance from the Store installation by "moving" it to its own directory.

By default, WSL distributions are installed into:

%UserProfile%\AppData\Local\Packages\<DistributionPackage>\

This includes the virtual filesystem, either:

  • WSL2: ..\<DistributionPackage>\LocalState\ext4.vhdx
  • WSL1: ..\<DistributionPackage>\LocalState\rootfs\

For Ubuntu, that <DistributionPackage> name is going to start with Canonical and include Ubuntu in the name (along with other identifiers).

It's worth checking to see if that directory is still there. If so, your data could be recoverable. However, I don't believe it's likely. It's more likely that, if you've rerun ubuntu.exe after the Reset, that it's the new installation that is there.

Anyway, I recommend this process for "disconnecting" the Store install so this doesn't happen again:

  • Once you've installed Ubuntu and configured your username and password.

  • Exit Ubuntu

  • From PowerShell:

    # Confirm the distribution name
    wsl -l -v
    # Distribution name may be "Ubuntu" or "Ubuntu-20.04" - Adjust the following lines accordingly:
    wsl --shutdown
    cd ~\Documents
    mkdir WSL\images
    mkdir WSL\instances\MyUbuntu
    wsl --export Ubuntu WSL\images\ubuntu_clean.tar
    wsl --import MyUbuntu WSL\instances\MyUbuntu WSL\images\ubuntu_clean.tar --version 2
    

    Note that most of the directory and filenames above (and the instance name) are up to you.

  • You should now have two Ubuntu installations, as you can confirm with wsl -l -v.

  • Launch the new one with wsl -d MyUbuntu

  • --imported instances don't "remember" your default username (although the user still exists). Set the default username in that instance via a /etc/wsl.conf file per this answer. Specifically:

    sudo -e /etc/wsl.conf
    

    And add the following lines:

    [user]
    default=<yourusername>
    
  • Exit the instance

  • Again, from PowerShell:

    wsl --terminate MyUbuntu
    wsl --set-default MyUbuntu
    
  • Now, the default instance is MyUbuntu. It is what will launch whenever you start WSL with the wsl command.

  • Uninstall the Store Ubuntu. You won't have to worry about it being reset again.

If you do need to try something out in Ubuntu that you feel might cause issues, do it in a new temporary instance:

Again, in PowerShell:

cd ~\Documents\WSL
mkdir instances\UbuntuTest
wsl --export MyUbuntu images\current_ubuntu.tar
wsl --import UbuntuTest instances\UbuntuTest images\current_ubuntu.tar --version 2
wsl -d UbuntuTest

That's a separate instance that you can install in, change configuration, go wild. When you are doing with it, exit and:

wsl --unregister UbuntuTest

This will delete that test instance, and leave MyUbuntu intact.

Note, of course, that the entire UbuntuTest instance will be gone, so make sure you don't create any documents or data that you need there longterm.

Vishnu 3333 avatar
in flag
Thank you so much for your time creating such a detailed answer. It really helps a lot. Like you said I had indeed rerun the Ubuntu.exe after the reset, so it's probably the new ubuntu filesystem that's sitting in '%UserProfile%\AppData\Local\Packages\' but still I attempted running file recovery in that folder with Recuva but couldn't recover any .vhdx file or rootfs folder (could 'reset' really have deleted the .vhdx file that effectively? Do you think it could be recovered?). A youtube video on this like you had mentioned, could get a good audience. I will try out your delink method.
NotTheDr01ds avatar
vn flag
@Vishnu3333 If you were truly using WSL2, then it would be the `.vhdx`, and in my experience, recovering files that large is pretty tough. I used to have fairly good success with Recuva when I was using magnetic storage, but really 0 success since moving to SSDs in the last few years. On the off chance that the instance was WSL1 (I know you said WSL2, but sometimes we just get lucky) then you would probably want to look for any filename you can remember from the instance, in the vent it became disconnected from the rootfs directory after deletion.
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.