<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zied Kharrat</title>
    <description>The latest articles on DEV Community by Zied Kharrat (@zk2k2).</description>
    <link>https://dev.to/zk2k2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3425562%2F269f2061-2624-46f5-b632-8644bb2a9426.png</url>
      <title>DEV Community: Zied Kharrat</title>
      <link>https://dev.to/zk2k2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zk2k2"/>
    <language>en</language>
    <item>
      <title>A Complete Guide to Removing Linux Disk Bloat</title>
      <dc:creator>Zied Kharrat</dc:creator>
      <pubDate>Wed, 13 Aug 2025 11:31:03 +0000</pubDate>
      <link>https://dev.to/zk2k2/a-complete-guide-to-removing-linux-disk-bloat-5102</link>
      <guid>https://dev.to/zk2k2/a-complete-guide-to-removing-linux-disk-bloat-5102</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're reading this, you probably use Linux pretty regularly. Who wouldn't? For anything software, Linux makes our lives much simpler. &lt;/p&gt;

&lt;p&gt;However, chances are that your Linux instance suffers from terrible bloat due to accumulated months (years?) of &lt;code&gt;apt install&lt;/code&gt; like there's no tomorrow, chunky Docker images that you forgot about, and that one web development project from your college class that, while not very flashy, is still home to a 500 MB &lt;code&gt;node_modules&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;This will inevitably lead to slower performance, longer boot times, and overall system instability. Before you know it, everything is in the red and screaming at you for help.&lt;/p&gt;

&lt;p&gt;Well, the good news is that it can be fixed.&lt;/p&gt;

&lt;p&gt;This guide will feature cleaning tips for native Linux users as well as WSL2 users, specifically &lt;strong&gt;Ubuntu 24.04&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Before we begin, please keep in mind that most deletions here are irreversible, so it is wise to backup any important data and double-check before proceeding.&lt;/p&gt;

&lt;p&gt;So without further ado, let's dive in!&lt;/p&gt;




&lt;h2&gt;
  
  
  What causes the bloat?
&lt;/h2&gt;

&lt;p&gt;I've identified 5 main issues that cause disk space loss on Linux environments. Chances are you have at least 3 of these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unused Docker images&lt;/strong&gt;: By far the most common, they can span several GBs in size, and are very easy to forget about&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Old/orphaned apt packages&lt;/strong&gt;: Remember that obscure library you used for that one lab? It's still very much alive and well in your system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Old project repos&lt;/strong&gt;: Especially JS-based projects, &lt;code&gt;node_modules&lt;/code&gt; folders can get very heavy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Accumulated log files&lt;/strong&gt;: You'd be amazed to know that daemons like &lt;code&gt;systemd&lt;/code&gt; and &lt;code&gt;sshd&lt;/code&gt; log anything and everything&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cache and temporary files&lt;/strong&gt;: &lt;code&gt;apt&lt;/code&gt; cache accumulates over time, so do temporary files, &lt;code&gt;.tar&lt;/code&gt; archives, etc…&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Fixing the problem 
&lt;/h2&gt;

&lt;p&gt;In this section, we will fix each of the previously mentioned issues, one by one:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Unused Docker images
&lt;/h3&gt;

&lt;p&gt;It's important to first distinguish between an &lt;strong&gt;unused&lt;/strong&gt; image and a &lt;strong&gt;dangling&lt;/strong&gt; image. &lt;/p&gt;

&lt;p&gt;A dangling image is untagged and is not referenced by any container. Unused images include dangling images, plus tagged images that no container is using (careful, a stopped container STILL uses its image!).&lt;/p&gt;

&lt;p&gt;You can start by taking a look at all your images:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your output will look similar to this. You can see that I have several heavy images on my system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydbccfejp2377oxw19li.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydbccfejp2377oxw19li.png" alt="Docker images" width="782" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can then run the following command to take a look at your containers (omitting the &lt;code&gt;PORTS&lt;/code&gt; column for better readability):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.CreatedAt}}\t{{.Status}}\t{{.Names}}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn4hiugqnk5pku8g88wz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn4hiugqnk5pku8g88wz.png" alt="Docker containers" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that I have several stopped containers from weeks ago, leftover from labs, old builds, and more.&lt;/p&gt;

&lt;p&gt;Now that you've taken a look, we can start cleaning! Start by running this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker container prune -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be prompted to confirm with &lt;code&gt;[y/N]&lt;/code&gt;. Careful! This will delete all stopped containers; make sure there aren't any you're using!&lt;/p&gt;

&lt;p&gt;Now let's list containers again;  all except one are gone!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67ze7bni7aa9ulmifbkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67ze7bni7aa9ulmifbkn.png" alt="Docker containers" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can remove the unused images, run the following: (Note that the &lt;code&gt;-a&lt;/code&gt; flag will remove all unused images, without it we only remove dangling images).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image prune -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that all is gone except the &lt;code&gt;kindest/node&lt;/code&gt; image (attentive readers will notice that while it's untagged, it's still being used by a container, so it's not dangling). Oh and we reclaimed 2 GB in space, woohoo!&lt;/p&gt;

&lt;h4&gt;
  
  
  Bonus: Unused Docker volumes!
&lt;/h4&gt;

&lt;p&gt;Docker volumes are persistent storage areas that Docker manages outside a container's writable layer. Note that they remain even after a container is deleted.&lt;/p&gt;

&lt;p&gt;Run the following to learn about volumes in your system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker volume ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03q1290ekxiwaxeisjfk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03q1290ekxiwaxeisjfk.png" alt="Docker volumes" width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're unsure about deleting a volume, you can run the following to learn more about it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker volume inspect &amp;lt;volume_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you're done checking, run this command to remove all unused volumes (we can add the -f flag to skip the confirmation prompt, but be careful as&lt;br&gt;
this will irreversibly delete unused volumes).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker volume prune -af
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that we reclaimed a solid 343 MB, nice!&lt;/p&gt;

&lt;p&gt;Finally, let's look at our Docker filesystem's disk usage run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker system df
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like there's nothing left to reclaim here; we can move on to the next step!&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Old/orphaned apt packages
&lt;/h3&gt;

&lt;p&gt;It's so easy to install packages on Linux-based systems, which is why they accumulate so quickly over time. Let's take a look at our manually installed packages, run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt-mark showmanual | while read pkg; do
  size=$(dpkg-query -W --showformat='${Installed-Size}\n' "$pkg" 2&amp;gt;/dev/null)
  if [ -n "$size" ]; then
    size_mb=$(echo "scale=2; $size/1024" | bc)
    echo -e "$pkg\t${size_mb} MB"
  fi
done | sort -k2 -nr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This very neat script will retrieve your manually installed packages as well as their sizes in kilobytes, convert these sizes to megabytes, then display each package with its size in descending order. Here's a sample output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvwie896i0xoopk2y878.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvwie896i0xoopk2y878.png" alt="Script output" width="667" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's up to you to decide which packages are worth deleting. When you do, just run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt purge pkg1 pkg2 pkg3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we can use &lt;code&gt;remove&lt;/code&gt; instead of &lt;code&gt;purge&lt;/code&gt;, which keeps the package's config files (useful if you think you'll need to reinstall it later).&lt;/p&gt;

&lt;p&gt;Finally, we can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt autoremove
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will clean up dependencies installed for packages you just removed.&lt;/p&gt;

&lt;p&gt;That's pretty much it for &lt;code&gt;apt&lt;/code&gt; packages, we can move onto the next section!&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Old project repos
&lt;/h3&gt;

&lt;p&gt;This one is pretty straightforward, you've probably worked on some projects along the way (or else why are you here?). &lt;/p&gt;

&lt;p&gt;Most of these projects will have dependencies or virtual environments that can get pretty heavy (especially JavaScript-based projects and Python virtual environments).&lt;br&gt;
 &lt;br&gt;
All you need to do is navigate to the directory where your projects are located. Then, you can use the command below to know a folder's size before deciding to delete it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;du -sh path/to/directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, I've contributed to Grafana, a big open-source project, you can see that the repo size is pretty significant (1.2 GB!).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffpkg6xmf05g0jjv2icct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffpkg6xmf05g0jjv2icct.png" alt="Command output" width="732" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All you have to do is use the following command to delete unused projects (but make sure to commit them to version control first!). Always double-check before using &lt;code&gt;rm -rf&lt;/code&gt;, unless you want bad surprises!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm -rf path/to/directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously, this applies to all directories, not just projects, so make sure to check other folders as well. Anyway, next section!&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Accumulated log files
&lt;/h3&gt;

&lt;p&gt;Linux logs everything: System events, logins, cron jobs, application logs, and many more. It's useful for debugging, monitoring and auditing, but it can get pretty heavy. Most logs live under &lt;code&gt;/var/log&lt;/code&gt;, so we'll focus on that directory, let's run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo du -sh /var/log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see below, the logs can get pretty heavy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidxfmw5lzfo8d73bbgev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidxfmw5lzfo8d73bbgev.png" alt="Logs size" width="662" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, before you go ahead and &lt;code&gt;rm -rf&lt;/code&gt; that directory to oblivion, note that many services &lt;strong&gt;depend on the files in that directory to exist&lt;/strong&gt;. And while it will probably be recreated eventually, said recreation isn't guaranteed to be immediate. Plus, daemon behavior is variable, so permissions and ownership can change, which will cause further access and security issues.&lt;/p&gt;

&lt;p&gt;What we can do instead is use the command below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo truncate -s 0 /var/log/*.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What &lt;code&gt;truncate&lt;/code&gt; does is essentially empty the file (its size becomes 0B), but keeps it so that processes can keep writing it without interruption.&lt;br&gt;
You can also do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo rm /var/log/**/*.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove old logs compressed as &lt;code&gt;.gz&lt;/code&gt; files. Please double-check and make sure you don't actually need these archives (for auditing or archiving purposes, for example).&lt;/p&gt;

&lt;p&gt;You can also run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo journalctl --vacuum-time=2d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove all logs aged 2 days or older. Below is a variant that also deletes older logs until the journal size reaches a desired value (Be careful if you need the old logs for audits!).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo journalctl --vacuum-size=100M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that I ran it and saved upwards of 600 MB of space!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftttil4ktuiffh432pqdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftttil4ktuiffh432pqdw.png" alt="Command output" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, it is recommended to use the following command to force periodic rotation and cleanup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo logrotate -f /etc/logrotate.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Cache and temporary files
&lt;/h3&gt;

&lt;p&gt;These are mainly &lt;code&gt;.deb&lt;/code&gt; files from the package cache stored in &lt;code&gt;/var/cache/apt/archives/&lt;/code&gt;. Very straightforward to clean, all we need to do is run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get clean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the before and after below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmw4vrpi0shez10l8sv50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmw4vrpi0shez10l8sv50.png" alt="Command output" width="712" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And don't forget to clean the &lt;code&gt;~/.cache&lt;/code&gt; directory from time to time too, it can get pretty heavy, especially if you work a lot with virtual environments in Python. Just see mine below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxf8bk1ot9ilzpk8j0zz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxf8bk1ot9ilzpk8j0zz.png" alt="Command output" width="677" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may experience some slower downloads after deleting cache (especially if you download the same packages over and over in different virtual environments), it's up to you to see if it's worth deleting!&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Bonus: Optimizing VHDX (WSL2 only)
&lt;/h3&gt;

&lt;p&gt;On WSL2, the &lt;code&gt;.vhdx&lt;/code&gt; file is basically a Virtual Hard Disk that stores your Linux filesystem. It can grow, but doesn’t automatically shrink when you delete files.&lt;/p&gt;

&lt;p&gt;To optimize it (reduce its size), we can compact the VHDX manually. Start by cleaning up inside WSL. The three commands below clear cached package files and metadata to free space, then trim unused blocks on the SSD to optimize performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt clean
sudo rm -rf /var/lib/apt/lists/*
sudo fstrim -v /
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll find that it says that 940 GB was trimmed. This might be surprising if, like me, you don't even have that much space on your physical hard drive. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkoi7nq6qpafp2nco81m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkoi7nq6qpafp2nco81m.png" alt="Command output" width="800" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In reality, WSL2’s VHDX has a 1 TB default virtual size (the maximum it can grow to), so Linux sees all that space when trimming, even if your Windows disk is smaller.&lt;/p&gt;

&lt;p&gt;Now, let's run PowerShell as an Administrator and shut-down WSL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl --shutdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the following to list your distros:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsl -l -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure that the WSL instance is stopped before proceeding any further. If it's not, double-check that you've closed all editors and killed all relevant processes.&lt;/p&gt;

&lt;p&gt;Then, run this to get the exact path of your &lt;code&gt;.vhdx&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-ChildItem "$env:LOCALAPPDATA\Packages" -Recurse -Filter ext4.vhdx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your output should look like this, go ahead and copy that path:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehhpd1pc2ep0wqsy9e42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehhpd1pc2ep0wqsy9e42.png" alt="Command output" width="800" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, run the next command to enter &lt;code&gt;diskpart&lt;/code&gt;, the command-line utility in Windows to manage disk partitions and volumes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;diskpart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, select the .vhdx disk file using the path we previously retrieved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select vdisk file="C:\Users\&amp;lt;you&amp;gt;\AppData\Local\Packages\&amp;lt;DistroPackage&amp;gt;\LocalState\ext4.vhdx" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get confirmation that the virtual disk file was correctly selected:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz619cvwcmnjtdya29rsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz619cvwcmnjtdya29rsk.png" alt="Command output" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can then run the following command to get more info about the virtual disk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;detail vdisk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see a similar output to this. What's relevant here is that the WSL2 disk can grow up to 1 TB maximum &lt;strong&gt;(virtual size)&lt;/strong&gt; but only 72 GB are currently in use &lt;strong&gt;(physical size)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F33pci3mthet5d7g4wvbz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F33pci3mthet5d7g4wvbz.png" alt="Command output" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's mount the disk so that Windows can access it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;attach vdisk readonly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, you will get confirmation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vz6gkzdr8p1id1qqzd7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vz6gkzdr8p1id1qqzd7.png" alt="Command output" width="665" height="93"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, we can safely run this to start the compaction process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compact vdisk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will get confirmation that the operation ran successfully:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felodv0oi7npypixmxe76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felodv0oi7npypixmxe76.png" alt="Command output" width="800" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can run detail vdisk again to check how much space we reclaimed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40bggcunzwh8xea79oi4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40bggcunzwh8xea79oi4.png" alt="Command output" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my case, it's a very solid 4 GB!&lt;/p&gt;

&lt;p&gt;Finally, detach the virtual disk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;detach vdisk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And exit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this final section, well done!&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;Well, that pretty much does it! I hope you cleaned up a few GBs along the way! &lt;/p&gt;

&lt;p&gt;Remember, it's important to periodically clean-up your system to avoid heavy disk fragmentation and performance degradation. &lt;/p&gt;

&lt;p&gt;I hope this article was helpful, good luck and until next time!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>webdev</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
