The Friendly Guide: “Why WSL is Eating My C: Drive (and How to Get it Back)”


If you’ve ever used the Windows Subsystem for Linux (WSL) with tools like Docker or Node.js, you may have been horrified to see your C: drive space rapidly vanish. Even more confusing, a tool like tree might show a file named \\wsl.localhost\Ubuntu\proc\kcore taking up a ridiculous amount of space—like 120TB!

This is a common and incredibly misleading problem. I recently went through this myself, and here’s the straightforward guide to understanding what’s happening and how to fix it.



💡 The Big Secret: The 120TB Is an Illusion

First, let’s clear up the biggest piece of misinformation:

  • The kcore file is not real. It’s a virtual file that represents a memory map of the Linux kernel. Its reported size (120TB or more) is a theoretical limit for a 64-bit system, not actual disk usage. It’s the equivalent of a ghost in the machine—it’s there, but it’s not taking up any space.

  • The real culprit is the WSL virtual hard disk (.vhdx). All of your WSL data—your Linux files, Docker images, npm packages, everything—is stored in a single, large virtual hard disk file on your C: drive. This file grows as you add more data, and crucially, it never shrinks on its own when you delete files.

This is why you can run a docker system prune, free up space inside WSL, and yet see no change on your Windows C: drive.



🛠️ The Fix: The Two-Step “Clean & Shrink” Method

To reclaim your lost disk space, you must do two things:

  1. Clean up the unnecessary files inside your WSL environment.
  2. Compact the virtual hard disk file to return the freed-up space to Windows.

Let’s dive in.



Step 1: Clean Up Inside WSL (Deleting the Bloat)

This is about identifying and removing the largest files from your Linux environment.

  1. Remove Docker Bloat:
    Even if docker system prune reclaimed 0B, large images you’re not using might be the problem.

    # List all Docker images by size
    docker images -a --format "{{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k4 -h
    
    # Identify and remove any large, unused images by their ID
    docker rmi  
    
  2. Find and Delete node_modules Folders:
    node_modules folders are notorious for their size. Navigate to old projects and simply delete them.

    # Example:
    cd ~/my-old-project/
    rm -rf node_modules
    
  3. Use ncdu to find the largest directories:
    This is my favorite trick. ncdu is a fantastic command-line tool that lets you navigate and find the biggest directories quickly.

    # Install if you don't have it
    sudo apt install ncdu
    
    # Run it to scan your entire file system.
    # Be patient, this might take a minute!
    sudo ncdu /
    

    Once the scan is complete, you can use your arrow keys to navigate and find the largest folders to investigate and clean up.



Step 2: Compact the VHDX File (The Magic Trick)

Now that you’ve deleted files inside WSL, you must tell Windows to physically shrink the container file on your C: drive.

  1. Locate Your WSL Disk File:
    This file is buried deep in your AppData folder. The path will look something like this:

    C:\Users\<YourUsername>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_...\LocalState\ext4.vhdx
    

    The folder name with CanonicalGroupLimited.UbuntuonWindows_ will have a unique string of characters for your system.

  2. Shut Down WSL:
    This is the most critical step. You must terminate all running WSL processes.

  * Open **PowerShell** or **Command Prompt** as an **Administrator**.
  * Run the shutdown command:
Enter fullscreen mode

Exit fullscreen mode

    ```bash
    wsl --shutdown
    ```
Enter fullscreen mode

Exit fullscreen mode

  1. Run the Compacting Utility:
  * In the same **Administrator** window, start the disk utility:
Enter fullscreen mode

Exit fullscreen mode

    ```bash
    diskpart
    ```
Enter fullscreen mode

Exit fullscreen mode

  * Now, select your WSL disk file (paste the full path you found in Step 1):
Enter fullscreen mode

Exit fullscreen mode

    ```bash
    select vdisk file=""
    ```
Enter fullscreen mode

Exit fullscreen mode

  * Finally, run the compact command:
Enter fullscreen mode

Exit fullscreen mode

    ```bash
    compact vdisk
    ```
Enter fullscreen mode

Exit fullscreen mode

This process will take a few minutes, depending on the size of your file. Once it’s complete, check your C: drive. You should see your free space magically restored!



Source link

Leave a Reply

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