Published on August 17, 2024By DeveloperBreeze

How to Free Up Disk Space on Ubuntu: A Comprehensive Guide to Clearing System Cache and Unnecessary Files

Over time, Ubuntu systems can accumulate a lot of unnecessary cache and temporary files that take up valuable disk space. This tutorial will guide you through several methods to clear useless cache, including system caches, old package files, and unnecessary Snap revisions.

1. Clear the PageCache, Dentries, and Inodes

These caches are managed by the Linux kernel and can be safely cleared using the following command:

sudo sync; sudo sysctl -w vm.drop_caches=3

  • sync: Writes any data buffered in memory out to disk.

  • vm.drop_caches=3: Clears the PageCache, dentries, and inodes.

2. Clear the Apt Cache

The APT cache contains downloaded packages that have been installed on your system. Clearing this cache can free up significant space:

sudo apt-get clean

This command removes everything under /var/cache/apt/archives/.

3. Remove Old Kernels

Old kernels can take up a lot of space on your system. To remove old, unused kernels, use the following command:

sudo apt-get autoremove --purge

This command automatically removes unused packages, including old kernels.

4. Remove Orphaned Packages

Orphaned packages are those that were installed as dependencies for other packages but are no longer required. You can remove them using:

sudo apt-get autoremove

5. Clear Thumbnail Cache

Ubuntu stores thumbnails for images and videos in a cache. Over time, this cache can grow large. You can clear it with:

rm -rf ~/.cache/thumbnails/*

This command removes all thumbnails from the cache.

6. Clear Systemd Journal Logs

Systemd keeps logs of system messages, and over time these can accumulate. You can clear old logs while keeping recent ones:

sudo journalctl --vacuum-time=2weeks

This command keeps logs from the last two weeks and removes older ones.

7. Clear Unused Snap Revisions

Snaps can accumulate multiple revisions, which can take up a lot of space. You can remove old, disabled revisions with the following script:

Create a script file:

nano clear_old_snaps.sh

Copy and paste the following script:

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

Save the file and close the editor.

Make the script executable:

chmod +x clear_old_snaps.sh

Run the script:

./clear_old_snaps.sh

Note: Ensure that all Snap applications are closed before running the script.

Conclusion

By following this tutorial, you can clear various types of cache and free up significant disk space on your Ubuntu system. Regularly performing these steps will help keep your system clean and efficient.

Comments

Please log in to leave a comment.

Continue Reading: