All Posts programming How to Free up Disk Space on Ubuntu Linux ?

How to Free up Disk Space on Ubuntu Linux ?

ยท 1205 words ยท 6 minute read

Empty Trash ๐Ÿ”—

Empty the trash can.

empty the trash can

Or use this command to empty the trash.

rm -r ~/.local/share/Trash/info/ && rm -r ~/.local/share/Trash/files/  

Unwanted screenshots ๐Ÿ”—

Delete unwanted screenshots and screencasts if you have any.

Remove Unused Packages ๐Ÿ”—

Use this command

sudo apt-get autoremove
# or
sudo apt autoremove

to remove packages that were automatically installed to satisfy dependencies for some package and are now no longer needed. This includes old Linux kernels that were installed during system upgrades.

Clean the APT Cache ๐Ÿ”—

Ubuntu keeps downloaded update packages in a cache. You can remove these using:

sudo apt autoclean && sudo apt clean
  • autoclean removes unnecessary packages.
  • clean removes all downloaded packages.

Remove Unused Applications ๐Ÿ”—

Uninstall applications you don’t use with:

sudo apt remove <package_name>
# or
sudo apt-get remove <package_name>

Replace <package_name> with the actual application name.

Use the following command to see list of all installed packages, sorted by size. If you see something big and don’t use it - uninstall it.

dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less

Identify Large Files and Folders ๐Ÿ”—

Use tools like:

  • Baobab (GUI) - Analyzes disk usage visually.
  • ncdu (terminal) - Shows folder sizes like Baobab in the terminal.
  • find / -type f -size +1024k - Search for files larger than 1024 KB.

Remove Old Kernels ๐Ÿ”—

You can usually keep the latest kernel and remove older ones. List kernels with:

ls -lh /boot

Remove them one by one (replace <version> with the actual kernel version):

sudo rm -rf /boot/vmlinuz-<version>

unused language packs ๐Ÿ”—

Remove unused language packs with sudo apt install localepurge.

Find and Remove Duplicate Files ๐Ÿ”—

Duplicate files can take up unnecessary space. Tools like FSlint (GUI) or FDUPES (command line) can help identify and remove duplicate files.

Systemd Journal Cleanup ๐Ÿ”—

On systems using systemd, you can free up space by removing old archived journal files with

sudo journalctl --vacuum-size 10M

adjusting the size parameter as needed.

Stay Updated ๐Ÿ”—

Keep your system and packages updated. Sometimes, updating packages can free up space rather than consuming more.

Run Bash script to remove unused snaps ๐Ÿ”—

You can use this script to clean all older versions of snaps from your computer. Save the script to a file, give it execute permission and run it:

Make sure you close all snaps before running this script.

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

remove marked for removal but still have configuration files ๐Ÿ”—

This command is designed to purge (completely remove) packages that have been marked for removal but still have configuration files left behind.

dpkg -l | grep ^rc | cut -d ' ' -f3 | xargs sudo apt-get purge -y

clean up Gradle ๐Ÿ”—

Your Gradle Home directory contains wrapper, caches and daemons files. The more projects with diffferent gradle versions the more subdirectories within those three directories. You can delete all three directories. This saved me a cool ~10GB of disk space.

Just use these two commands.

cd ~/.gradle
rm -rf caches daemon wrapper

clean up Android SDK ๐Ÿ”—

Android System images are only used by emulators. If you use a real Android device during development for debugging, you no longer need them, so you can remove them all. This could easily save you 5GB doing this.

clean unwanted android sdk

Delete Leftover Android Studio IDE Directories ๐Ÿ”—

I noticed that there are many leftover old versions of Android Studio in cache on my Ubuntu 24.04 LTS laptop. Here are all of them:

~/.cache/Google โญ ll
total 40K
drwxrwxr-x 31 ubuntu ubuntu 4.0K May 20  2025 AndroidStudio2024.3/
drwxrwxr-x 32 ubuntu ubuntu 4.0K Jun 25  2025 AndroidStudio2024.3.2/
drwxrwxr-x 33 ubuntu ubuntu 4.0K Aug  1  2025 AndroidStudio2025.1.1/
drwxrwxr-x 32 ubuntu ubuntu 4.0K Sep  3 06:11 AndroidStudio2025.1.2/
drwxrwxr-x 32 ubuntu ubuntu 4.0K Oct 13 01:09 AndroidStudio2025.1.3/
drwxrwxr-x 28 ubuntu ubuntu 4.0K Nov 13 11:30 AndroidStudio2025.1.4/
drwxrwxr-x 26 ubuntu ubuntu 4.0K Dec 17 08:05 AndroidStudio2025.2.1/
drwxrwxr-x 26 ubuntu ubuntu 4.0K Jan 19 17:38 AndroidStudio2025.2.2/
drwxrwxr-x 27 ubuntu ubuntu 4.0K Feb 11 03:15 AndroidStudio2025.2.3/
drwxrwxr-x 23 ubuntu ubuntu 4.0K Feb 20 13:37 AndroidStudio2025.3.1/

~/.cache/Google โญ du -hd 1 2> /dev/null | sort -hr
14G .
2.3G ./AndroidStudio2025.1.3
2.2G ./AndroidStudio2025.1.2
1.8G ./AndroidStudio2024.3.2
1.5G ./AndroidStudio2025.1.4
1.5G ./AndroidStudio2025.1.1
1.3G ./AndroidStudio2025.2.1
1.2G ./AndroidStudio2025.2.3
938M ./AndroidStudio2025.2.2
685M ./AndroidStudio2025.3.1
440M ./AndroidStudio2024.3

To get rid of those old Android Studio cache versions in Ubuntu 24.04, the correct and safest way is to use Android Studio IDE’s built-in cleanup tool.

When you upgrade to a major new version, Android Studio often keeps the old cache, settings, and logs from previous versions in case you want to roll back.

  • Open the latest version of Android Studio.
  • Go to Help > Delete Leftover IDE Directories.
  • A dialog will appear showing the locations and sizes of unused directories for previous versions. Select the versions you no longer use and click Delete.

I reclaimed ~13GB of storage disk space with this Android studio old versions cleanup!

Logs ๐Ÿ”—

Logs are the logging of events of programs and apps on your Ubuntu Linux. Old logs are obsolete. This command will delete all logs everywhere in your Ubuntu Linux OS.

find ~/ -type f 2> /dev/null | grep '\.log$' | sed 's/[[:space:]]/\\\ /g' | xargs rm

remove unused docker images ๐Ÿ”—

If you are using docker for your local development, use this command docker image prune to delete obsolete Docker images downloaded on your machine.

docker image prune

That saved me +2GB of storage.

development libraries & programs ๐Ÿ”—

  • delete projects, libraries and programs installed by Python’s pip and pip3.

  • delete projects, libraries and programs installed by NodeJS’s npm.

  • delete projects, libraries and programs installed by deno.

  • delete unwanted hidden cache files of ~/.pip , ~/.android , ~/.composer , ~/.tesseract , ~/.gradle , ~/.cocoapods , ~/.matplotlib , ~/.local , ~/.docker and other hidden cache directories which are located in the home directory (/home or just ~).

  • delete unwanted hidden cache files in ~/.npm using this command.

npm cache clean --force
  • delete unwanted hidden cache files in ~/.pnpm using this command.
pnpm cache delete

Note: After you delete cached files and modules, the modules you use will get cached again. So, the deletion of cache is to remove old obsolete modules.

other helpful commands ๐Ÿ”—

Get all directories sorted by size.

du -hd 1 2> /dev/null | sort -hr

Get all files & directories, sorted by size.

ls -AhlS

Get all files which have sizes more than 500MB.

find / -type f -size +500M 2> /dev/null | xargs du -h 2> /dev/null

Get all error log files ending with .err extension.

find / -type f 2> /dev/null | grep '\.err$' | xargs du -h 2> /dev/null

You can empty out the files instead of deleting them by this command.

echo "" > /var/log/example_file.log

But if you still want to delete a file, use this command.

rm /var/log/example_file.log

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .