8

So here is the problem: I'm using Ubuntu 20.04 on WSL2 on Windows 10, and I noticed that after removing files on Ubuntu I was not getting the space back that was taken up by the removed files. For example: Before I deleted files on Ubuntu it showed on Windows explorer that my free space on the (C:) drive was around 46GB. Then after I deleted around 10GB of files, which in my case where some temporary Gatsby sites, it resulted in even less free space, around 45GB. Which I thought was very weird.

So I guess my question is: How can I get back those unused bits? Is there some terminal command which I can use or can I do something via the windows gui or something.

PS My previous question got closed for a lack of detail. I hope this time around it will suffice. Also please do not downvote this without some comment on why, but at least give me some information as to why.

9

There's a WSL Github issue open on this topic. WSL will automatically grow the virtual disk (ext4.vhdx), but shrinking it to reclaim unused space is something that must currently be done manually.

The first thing you'll need to do is know the location of your ext4.vhdx. For a default Ubuntu installation, it should be in something like %PROFILE%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\

Then there are several techniques that you can use to remove the unused space. I recommend you start with a wsl --shutdown and copy the vhdx as a backup to start.

  • If you are on Windows Professional or higher, you can install Hyper-V and use the Optimize-VHD commandlet as described in the original issue. .

  • On Windows Home (and higher) you can use diskpart as described in this comment.

  • Exporting the WSL distro and re-importing it into a new WSL instance (as in this comment) will also reclaim the space. Note that you will need to reset the default username after an import. See here.

I have tested and confirmed both the second and third techniques personally.

2
  • 2
    I accept your answer as I have successfully purged my distro with 2GB using the first method without any problems. For anyone else reading this is, I first enabled Hyper-V in Windows Features because I have windows 10 pro. Then after a restart needed for enabling hyper-v I did wsl --shutdown in powershell. And because I enabled Hyper-V I could run Optimize-VHD -Path C:\Users\user\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx -Mode Full – Timothy Dec 27 '20 at 12:53
  • 1
    After a ubuntu diskpart, I went from 5.15 to 5.12 gb vhdx. Then, a export/import went from 5.12 to 4.27 gb. So I'll argue that export/import is more efficient in reclaiming space. – trogne Apr 12 at 22:18
0

With this Self-elevate script you can compact multiple vhdx at once.

# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
    if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
        $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
        Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
        Exit
    }
}

#Full Path of vhdx file
$d1 = #Ex: "E:\Docker\wsl\data\ext4.vhdx"
$d2 = ""
$d3 = ""
$d4 = ""
$d5 = ""
$d6 = ""

$paths = $d1, $d2, $d3, $d4, $d5, $d6

foreach ($file in $paths) {
    echo ""
    echo "Befor Shrinking File sizes in MB"
    Get-ChildItem -Path $file | Select-Object FullName, @{Name = "Size"; E = { $_.Length / 1MB } }
    echo ""
    echo "---------"
    echo "Shrinking $file"
    echo "---------"

    wsl --shutdown
    Optimize-VHD -Path $file -Mode Full

    echo ""
    echo "After Shrinking File sizes in MB"
    Get-ChildItem -Path $file | Select-Object FullName, @{Name = "Size"; E = { $_.Length / 1MB } }
    echo ""
    echo ""
    echo ""
    echo ""
}

Read-Host -Prompt "Press Enter to exit"

btw, With a little tweak you can also run it on startup ("Start Docker when you login" should be disabled)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.