How to Clean up Disk Space on Ubuntu 22.04


Space is the major concern of the user on Ubuntu or any other operating system. What’s important is, how good the operating system is to react/deal in such a situation.

Ubuntu, being the loved Linux distribution offers optimization support to clean up disk space on Ubuntu. Like other Linux systems, Ubuntu also relies mostly on the command line operations and it offers various commands to free up disk space. Moreover, it also has an interactive GUI which has a built-in application to clean up the disk space on Ubuntu.

All in all, we are focused on listing all the command line and GUI methods to clean up disk space on Ubuntu 22.04.

Outline:

Let’s start with command line methods:

Command Line Methods to Clean Up Disk Space on Ubuntu 22.04

Ubuntu has tens of commands that optimize the space. Disk space optimization includes removing unused packages’ cache, removing temporary files, and cleaning up various package manager’s cache. Here, we have listed some command line methods to free up disk space on Ubuntu:

Remove Unused Packages

When we remove any apt-based installation using only the “remove” option, some dependencies are left behind that are not considered by the “remove” option. To remove them, use the following command:

sudo apt autoremove

Remove Old Kernel/Configuration of Unused Packages

Like the dependencies, some useless configurations of the removed packages might be there on your system that are eventually consuming space. Remove such configurations using the below command:

sudo apt autoremove --purge

This also removes any old instances of the Linux Kernel, as well.

Clean up Packages/apt Cache

The apt is the primary repository of Ubuntu. It manages the “.deb” related packages and the associated files. As most of the packages are offered/managed by apt, thus the apt cache takes a large amount of space which increases based on the usage. It is suggested to keep an eye on the apt cache using the command:

sudo du -sh /var/cache/apt

Right now, the total apt-cache size is “106M”. To clean apt cache, you have two options:

  • Remove the outdated packages only from the apt (little or no impact):
sudo apt autoclean

As seen, the autoclean has removed nothing because there won’t be any outdated packages in the apt-cache.

  • Remove the entire apt cache (frees up more space than the above):
sudo apt clean

And the apt cache is now cleaned up. More space has been added to the system.

Remove Temporary Files

The temporary files are generated automatically as a supportive hand to any application. With the increase in number of applications/packages on Ubuntu, the “tmp” directory also increases in size.

By default, the temporary files are stored at the “/tmp/” location, which can be freed up using the command:

sudo rm -rf /tmp/*

Remove Unused Repositories

In Ubuntu, the “/etc/apt/sources.list.d/” directory contains the repository files (PPA and official sources) that are added to install any package. These files may be useless after the removal of the associated package. Thus, it is recommended to remove any It is recommended to “ls” inside the directory and look for useless/unused repository files:

Use the “rm” command to remove the file:

sudo rm <File-Path>

Or you can use the below command to remove the repository by its name:

sudo add-apt-repository --remove <repository-name>

Remove Snap-based Outdated Packages

Snap holds the outdated/old versions of the snap-based packages in the “/var/lib/snapd/snaps” directory. As the updates/number of snaps on the system increase, the older versions are kept eventually taking up space on the disk.

To free up the snap-based outdated packages, the following bash script is used:

#!/bin/bash
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snap rev; do
sudo snap remove "$snap" --revision="$rev"
done

Breakdown of the Script:

  • snap list –all | awk ‘/disabled/{print $1, $3}’: Search for the disabled snaps from the output of the “snap list” command and get the 1st and 3rd column of the “snap list” command (that matches the disabled keyword).
  • While loop: The name (1st) and rev(3rd) columns are stored in the “snap” and “rev” variables. Lastly, the “snap remove” command removes all these matches.

Make the script executable:

sudo chmod a+x <Path-of-the-Script>

Check the size of the snap cache using the below command before and after executing the script:

du -sh /var/lib/snapd/snaps

./snapclean.sh

du -sh /var/lib/snapd/snaps

Look at the output, the size was 2.5 GB before cleaning and it became 1.6GB after running the script.

Clean up Flatpak Runtime/Cache

Flatpak runtime refers to the libraries and services required to run flatpak-based applications. These flatpak runtimes consume space which increases with the number of flatpak applications:

flatpak uninstall --unused

Clean up Journal logs

The journal logs carry the log data, i.e., Kernel messages, standard outputs, systemd errors, etc. This also consumes space and contributes to overall disk space usage.

journalctl -q --disk-usage

Set the vacuum size of the journal, i.e., the journal logs will automatically clear when the size reaches 120MB:

sudo journalctl --vacuum-size=120M

Locate and Remove Useless/Large Files and Directories

Some files and directories might consume a huge amount of disk space. You can manually search for those and find which one is important to keep and which one to delete.

  • Directories:

For instance, the command below sorts the top 5 largest directories on the system:

sudo du -sk * | sort -nr | head -5

You can change the head parameter to adjust the number of directories you want to get:

To remove the directories (non-empty), you can use the “rm -r” command:

sudo rm -r <Path-To-Directory>
  • Files:

The command below searches for the files having a size greater than 100MB:

sudo find / -type f -size +100M

The path to each large file is given, which you can remove using the rm command:

sudo rm <Path-of-the-File>

Empty the Trash

The Trash directory contains the temporary deleted files that remain in the trash bin for 30 days. The data is held for 30 days to recover if the files are accidentally removed. However, it takes space too which can be freed up using the command:

rm -rf ~/.local/share/Trash/*

These are the possible CLI-based methods. Let’s have a quick look at the GUI-based methods:

GUI Methods to Clean Up Disk Space on Ubuntu 22.04

Ubuntu has the default Disk Usage Analyzer to look for the total and available space on the system. You can also free up the disk space using Disk Usage Analyzer. Apart from this, various third-party tools are also available that assist in managing the disk space on Ubuntu.

Disk Usage Analyzer

Search for it in the applications and launch it. You will see the home directory of the currently logged-in user along with the root directory:

Click on the directory where you want to look for the unused files. It is recommended not to dig into the root directory for any removal. Here, we have navigated to the home directory. A directory named “.cache” is available which contains the cache of various packages/package managers. Right-click on it and move to trash:

The selected directory will be moved to the trash and now you have to remove it from the trash bin as well.

Third-Party Tools

Ubuntu offers a wide range of other tools to manage disk space on Ubuntu. Here we are listing a few, along with their installation commands and their usage.

  • Bleachbit

This tool cleans up the cache, internet history, cookies, and other temporary data. It offers two usage modes, i.e., a normal user and a root user. Use the below command to install it:

sudo apt install bleachbit

Upon its successful installation, launch the application (as a root or normal user) from the menu:

When launched, you will see the preferences dialog, configure it if required (recommended to go with the default). Otherwise, close it and proceed:

Select the options from the left pane and click on the “Clean” button to proceed:

It permanently deletes all the selected files/directories/options:

At the end, it also shows the total recovered space and the number of files deleted:

  • Sweeper

The sweeper tool allows you to remove recent documents, command history, thumbnail cache, and browsing-related data. Sweeper offers less functionality than Bleachbit but is effective enough to recover the space. Let’s first install it on Ubuntu:

sudo apt install sweeper

Once installed, launch it from the terminal and you will see the list of possible options that the sweeper can remove. Select a few of them or select all and then click on Clean Up to clean up the disk space occupied by these.

You will get the progress against your selected actions in the output pane, as can be seen below:

Other Tools

We cannot say that the above two tools are the best. Each tool has its pros and cons. We have provided a list of a few other GUI-based tools to manage space. You can install them and use any of these which serves your purpose perfectly.

Tools Installation Commands
Stacer sudo apt install stacer
Czkawka sudo snap install czkawka

This is all about cleaning your Ubuntu system!

Bottom Line

To clean up your Ubuntu system, you need to remove the unused packages, apt repository cache, snap/flatpak caches, logs, and trash directory. All these operations can be handled through the command line as well as GUI operations.

Moreover, you can use the built-in and third-party GUI-based applications to clean up the disk space on Ubuntu 22.04. This post has demonstrated all the possible methods to clean up disk space on Ubuntu 22.04.

Print Friendly, PDF & Email
Categories