Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Shrink the Virtual Disk of a Linux Distro in WSL

August 15, 2024
in A, Blog, WSL
0
ADVERTISEMENT

Table of Contents

When you delete files or free up space in your WSL distribution, the virtual disk doesn’t automatically shrink. To reclaim this unused space, you need to compact the virtual disk (VHDX) file.

Before you begin

Before you begin, make sure that your WSL instance is not running. You can check the status of your WSL distributions by executing the following command in PowerShell:

wsl.exe --list --verbose

If any distributions are running, shut them down using:

wsl --shutdown

Method 1: Using DiskPart (Admin Privileges Required)

1. The first step is locate the VHD file, the VHD file for your WSL distribution is typically located in:

C:\Users\<YourUsername>\AppData\Local\Packages\<DistroPackageName>\LocalState\

Replace <YourUsername> with your actual Windows username and <DistroPackageName> with the name of your Linux distribution package (e.g., CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc for Ubuntu).

5J6erdUtp5LNA2myvnxgKm4tKs0wNFRgM3MZXHzaKd2D74hvgCOZIHrEKGzO
3. Open PowerShell (Terminal) as administrator then type diskpart and press

4. Use the following command, replacing the path with the actual location of your VHDX file to select the VHDX file.

select vdisk file="<path_to_ext4.vhdx>"
eUKp3pLU9P8GclX5SGA4GZiPUK6N3ReyJLYHxmjbOWo70MyU8z11BeMaiFKj

5. Once the disk is selected, run the below command within the DiskPart prompt to shirk the disk. This will reclaim the unused space in your VHD file. You should see a message indicating that the disk has been successfully compacted

compact vdisk
6jf8mPLZObQLEAhPbAoD0ZKvOqo3OeTFzi3ZR4VrLtCe3W0lLumxiBpeHnrU

After the process is complete, you can check the size of the VHD file to confirm that it has been reduced.

cfcwjFxqNmcbh6hKlsWpalif0ROEA6kYEa2h9maEZapulSXSMoT1Mds7u91c

Method 2: Using Hyper-V

In case you’ve Hyper-V installed on your computer. You can use its cmdlet to compact the virtual disk of the WSL instance.

5vTmIzjURHOtRysq6zkI5LdMYcPCGzFqnoeGhYcpDNNda6zbX67KmBvaBmB5

Open PowerShell as administrator, then navigate to the location of the VHD file. The below screenshot is an example of the Ubuntu distribution.

5T0HFzD8kzgfvnQ6D3E1traQdc2xNl44q1awHW51JLRuS4wEkUzNz550hGI1

Run the following command to compact the VHD file:

optimize-vhd -Path .\ext4.vhdx -Mode full
eCYaLjzdKXnsouDvTKhdGdsbJrFxsWZLIfwx3rGQ6d0PQAJ69n9yGmESeQN9

You can monitor the processing status on the screen. After completion, verify the virtual disk size to confirm functionality.

Method 3: Using the WSLCOMPACT

And the last way, you can you a third-party tool to compact the disk.

  • WSLCOMPACT: https://github.com/okibcn/wslcompact

1. Install it using the below PowerShell commands:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
iwr -useb https://raw.githubusercontent.com/okibcn/wslcompact/main/setup | iex

2. Run the wslcompact command to see the current size and estimated size of all WSL instances.

3. Run the below command to compact the disk and apply the change to the VHD file.

wslcompact -c ubuntu
ALMpSPYWvwdBFfAv1tJKKVxsaO0samBMVi7R6Ny9kqVCpJE8UM7S4ECgSIEY

If the tool is no longer needed, to remove the utility, close all your PowerShell instances, open a fresh one, and type:

Remove-Item "$($env:PSModulePath.split(';')[0])/WslCompact" -Recurse -Force

Conclusion

By following methods, you should be able to effectively shrink the virtual disk of your Linux distribution in WSL, reclaiming valuable disk space on your Windows system.

ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Configure Memory Limits in WSL2

Next Post

Get-MsalToken: The Property Authority Cannot be Found on this Object

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory