You can recover your Ubuntu virtual machine data after uninstalling WSL2 by locating the VHDX disk image files stored in your user directory. These files persist even after the Windows Subsystem for Linux feature is removed from the system. By identifying the correct path and mounting the virtual hard disk, you can access your lost files or reinstall the environment using the preserved state.
Understanding Data Persistence in WSL2
When you uninstall the "Windows Subsystem for Linux" optional feature or remove the Ubuntu distribution via the Microsoft Store, the underlying virtual machine infrastructure is often left behind in a dormant state. Unlike standard applications that delete their configuration files upon removal, WSL2 stores its virtual hard disk (VHDX) files on your local NTFS file system. This design choice ensures that users do not lose their work if they accidentally remove the distribution or need to troubleshoot the host operating system. The critical component here is the ext4.vhdx file, which contains the entire Linux filesystem including your home directory, installed packages, and project files. Understanding this separation between the runtime engine and the storage layer is the first step toward recovery.
Locating Your Virtual Hard Disk Files
The most common location for these VHDX files is within the AppData folder of your current Windows user profile. You should navigate to
C:\Users\YourUsername\AppData\Local\Packages. Inside this directory, look for folders that start with the name of your Linux distribution, such as
CanonicalGroupLimited.UbuntuonWindows_.... Within that specific package folder, there is a subdirectory named
LocalState. This is where the heavy lifting occurs. Open the
LocalState folder, and you will typically find a file named
ext4.vhdx. In some configurations, particularly if you have multiple distributions or snapshots, you might also see a
swap.vhdx file, but the
ext4.vhdx is the primary container for your data. If you cannot find it here, check the default installation path at
C:\ProgramData\Microsoft\WSL, although this is less common for standard user installations.
Mount NFS share from WSL in Windows 10 is a related task that involves similar file system interactions, though it focuses on network sharing rather than local disk recovery.
Verifying File Integrity
Before proceeding with any restoration steps, verify that the
ext4.vhdx file has a non-zero size. If the file is empty or missing entirely, the data may have been permanently deleted during the uninstallation process, and recovery would require third-party data recovery tools rather than simple reinstallation. If the file exists and retains its previous size, your data is intact and ready to be mounted.
Reinstalling WSL2 to Access the VM
The most reliable way to restore your Ubuntu environment is to reinstall the WSL2 feature and the specific distribution. Open PowerShell as an Administrator and run the command
wsl --install. This command enables the necessary virtual machine platform features and installs a default Linux distribution. Once the installation completes, restart your computer to ensure all kernel changes take effect. After rebooting, launch the Ubuntu application from the Start menu. The system will initialize the new virtual machine. However, this fresh installation will not automatically link to your old
ext4.vhdx file because WSL creates a new, empty virtual disk for the newly installed instance.
To use your old data, you must manually import the existing VHDX file. This process requires using the
wsl --import command. First, create a dedicated folder on your drive where you want the new VM to reside, for example,
C:\WSL\Ubuntu-Recovery. Then, execute the following command in PowerShell:
wsl --import Ubuntu-Recovery C:\WSL\Ubuntu-Recovery C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_\LocalState\ext4.vhdx. Replace the paths with the actual locations found in the previous step. This command tells WSL to create a new distribution named "Ubuntu-Recovery" and attach your old virtual hard disk to it.
Accessing Your Recovered Data
Once the import process finishes, you can launch your recovered environment by running
wsl -d Ubuntu-Recovery. You are now inside the Linux shell with access to all your previous files, configurations, and installed software. You can verify this by checking your home directory or looking for specific project folders you remember working on. If you wish to make this your default distribution, you can set it using
wsl --set-default Ubuntu-Recovery. This approach allows you to continue working without losing any progress, effectively reversing the impact of the accidental uninstall.
Enable VM Platform Feature to Run WSL2 (Ubuntu) on Windows 11 in VirtualBox highlights the importance of the underlying virtualization features that make these operations possible across different Windows versions.
Frequently Asked Questions
Will reinstalling WSL2 delete my existing data?
No, reinstalling the WSL feature or removing a distribution does not automatically delete the VHDX files containing your data. These files remain on your disk unless you explicitly delete them or format the partition. Always backup your VHDX files before performing major system changes to ensure safety.
Can I mount the VHDX file directly in Windows Explorer?
Yes, you can mount the
ext4.vhdx file directly in Windows by right-clicking it and selecting "Mount." However, Windows cannot natively read the ext4 filesystem, so you will need third-party software like ExtFS for Windows to view the contents. Using WSL to import the disk is generally safer and preserves permissions better.
What if I cannot find the LocalState folder?
If you cannot find the
LocalState folder, ensure that hidden files are visible in File Explorer. Additionally, check if you previously moved your WSL data to a different drive using symbolic links or custom configuration settings. Reviewing your WSL configuration files may reveal alternative storage paths.
Is it safe to delete the old VHDX file after importing?
It is safe to delete the original
ext4.vhdx file only after you have successfully imported it into a new WSL distribution and verified that all your data is accessible in the new environment. Keep a backup copy until you are certain the import was successful and no errors exist in the recovered system.