The wsl --unregister ubuntu command does not delete the ext4.vhdx file; it only removes the WSL distribution registry entry. To recover your Linux environment, you simply need to locate the existing VHDX file on your Windows filesystem and re-register it using the wsl --install -d or wsl --set-version commands with the correct path.
Understanding What Happened
It is a common misconception that unregistering a distribution wipes the data. In reality, thewsl --unregister command is designed to clean up configuration metadata stored in the Windows Registry. It tells Windows that the specific distribution (in this case, "ubuntu") is no longer active or registered with the WSL subsystem. The underlying virtual hard disk, which contains your actual Linux files, directories, and applications, remains untouched on your storage drive. This design choice allows users to reset configurations without losing their work, provided they know where the disk image was stored.
The file you are looking for is named ext4.vhdx. It is located within the user-specific AppData folder structure. Because it is still present on the disk, the recovery process is straightforward and involves pointing WSL back to this existing file rather than trying to retrieve deleted data from a recycle bin or backup.
Locating the ext4.vhdx File
To find your file, you must navigate to the hidden AppData directory. Open File Explorer and enable "Hidden items" in the View tab if you do not see these folders. The default path for Ubuntu 20.04 and later versions installed via the Microsoft Store is:%LOCALAPPDATA%\Packages\CanonicalGroupLimited.Ubuntu...\LocalState\ext4.vhdx
If you installed an older version of Ubuntu or used a different provider, the package name might differ slightly, but the structure remains similar. Look for a folder starting with CanonicalGroupLimited followed by the specific Ubuntu version number. Inside the LocalState subdirectory, you will find the ext4.vhdx file. Its size will correspond to the amount of data stored in your previous Linux installation.
For users who have customized their installation location or used a portable setup, the file might be elsewhere. However, the standard installation path covers the vast majority of cases. Once you have confirmed the file exists and noted its full path, you can proceed to re-registration.
Re-registering the Distribution
With the path identified, you can restore the distribution by telling WSL to register the existing VHDX as a new instance. You cannot reuse the exact name "ubuntu" if a generic Ubuntu package is already partially registered, so it is often safer to register it with a temporary name first, then rename it if necessary. Open PowerShell or Command Prompt as Administrator. Run the following command, replacing the path with the actual location of yourext4.vhdx:
wsl --import UbuntuRecovery C:\WSL\UbuntuRecovery "C:\Path\To\Your\ext4.vhdx" --version 2
This command imports the VHDX into a new distribution named UbuntuRecovery. The second argument specifies the installation directory for the new distribution's metadata, which should be a new empty folder. The --version 2 flag ensures it uses WSL2, which is required for modern Ubuntu releases.
Once the import completes, you can verify the distribution is listed by running:
wsl -l -v
You should see UbuntuRecovery in the list with the status Stopped. If you prefer the name to be simply ubuntu, you can stop the distribution and then unregister the original placeholder if it exists, though keeping a distinct name avoids confusion during the transition.
Restoring Default User Permissions
After importing, the root user is typically the default. To match your previous experience, you may want to set a default user. First, start the distribution:wsl -d UbuntuRecovery
Inside the Linux shell, create a new user or identify your previous username. Then, set it as the default in the WSL configuration file. Edit /etc/wsl.conf and add:
[user]
default=your_username
Exit the shell and restart. Your environment should now mirror the state before the unregister command, with all files, packages, and settings intact.
Preventing Future Data Loss
To avoid accidental loss in the future, consider backing up yourext4.vhdx file regularly. Since it is a single large file, you can copy it to an external drive or cloud storage. Additionally, use wsl --export instead of --unregister if you plan to move or archive your distribution, as export creates a compressed tarball that is easier to manage across different machines. For more information on managing disk space and sparse files, you might find How to Shrink Sparse Files in Linux After Copying Them Without the -S Operator useful for optimizing your VHDX size.
Frequently Asked Questions
Does wsl --unregister delete my files?
No, it does not delete any files. It only removes the registry key associated with the distribution name. Theext4.vhdx file containing your data remains on the disk until you manually delete it or reinstall the distribution over it.
Can I recover data if I deleted the ext4.vhdx file?
If you manually deleted theext4.vhdx file after unregistering, recovery becomes difficult because the file system blocks are marked as free space. You would need specialized data recovery software, and success is not guaranteed. Always ensure the file exists before assuming it is safe.
Why does my new distribution show as Stopped?
Imported distributions are always created in a stopped state to save resources. You must start them manually usingwsl -d [Name] or configure them to start automatically if needed. This is normal behavior for newly imported VHDX files.
What if the ext4.vhdx is corrupted?
If the file fails to mount or shows errors, it may be corrupted. You can try runninge2fsck inside the Linux environment to check and repair the file system. Ensure you have a backup before attempting repairs, as faulty operations can worsen the issue.
Is there a difference between import and install?
Yes.wsl --install downloads and sets up a fresh distribution from Microsoft servers. wsl --import takes an existing disk image and registers it locally. Use import when recovering from a backup or moving a distribution, and install when setting up a new environment.