Skip to content

Instantly share code, notes, and snippets.

@Csqhi515
Created October 18, 2024 17:35
Show Gist options
  • Select an option

  • Save Csqhi515/7d62d9ba2f94e1f7a762e9e8a8429d55 to your computer and use it in GitHub Desktop.

Select an option

Save Csqhi515/7d62d9ba2f94e1f7a762e9e8a8429d55 to your computer and use it in GitHub Desktop.

Revisions

  1. Csqhi515 created this gist Oct 18, 2024.
    30 changes: 30 additions & 0 deletions CompactWSL.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    @echo off

    net session >nul 2>&1
    if %errorlevel% neq 0 (
    echo Requesting administrative privileges...
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
    )

    wsl sudo fstrim --all; echo "Exit status: $?";
    wsl --shutdown

    :: ========= Instructions =========
    :: Please define your list of files here.
    set fileList=("%LOCALAPPDATA%\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx" "%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx")

    :: Process each file in the list.
    for %%F in %fileList% do (
    echo Processing %%F using diskpart compact.
    (
    echo select vdisk file=%%F
    echo detach vdisk
    echo attach vdisk readonly
    echo compact vdisk
    echo detach vdisk
    ) | diskpart
    )

    echo Finished.
    pause