Disk Cleanup Ubuntu
Introduction
This guide explains some safe steps to clean up the hard drive on an Ubuntu computer..
Measure Disk Space
- To check the available space, execute the command
df.
df
In this example, the root partition (/dev/mapper/ubuntu--vg-ubuntu--lv) mounted at / is completely full (100% used).
harry@templateunbutu:/var/log$ df
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 812876 1076 811800 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 8408452 8273228 0 100% /
tmpfs 4064380 0 4064380 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1768056 268396 1391528 17% /boot
tmpfs 812876 4 812872 1% /run/user/1000
Steps
Step 1. - Remove apt cache
- Remove old cached package downloads.
sudo apt clean
Step 2. - Remove orphaned/unused packages
- Autoremove gets rid of packages no longer needed.
sudo apt autoremove --purge
Step 3. - Clear old log files
- Ubuntu logs are stored in
/var/log. Rotate logs older than 3 days and then clear the logs.
sudo journalctl --vacuum-time=3d
- That keeps only the last 3 days of system logs.
- Remove archived logs in
/var/log.
sudo rm -f /var/log/*.gz
sudo rm -f /var/log/*.1
sudo rm -rf /var/log/journal/*
Step 4. - Clear .cache Folder
- Clear the entire
.cachefolder. This is mostly rebuildable stuff like thumbnails, temp downloads, etc.
rm -rf ~/.cache/*
5. - Snap Cache
- Snap can grow silently. You can clean it up like this.
sudo du -sh /var/lib/snapd/cache
Step 6. - Check for large files
- Check what’s eating up space:
sudo du -ahx / | sort -rh | head -n 20
Summary
-
Confirm that the clean up has been successful by issuing the
dfcommand. -
In this example, we can see that the root partition (/dev/mapper/ubuntu--vg-ubuntu--lv), mount at
/used space has decreased from 100% to 80%.
harry@templateunbutu:/var/log$ df
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 812876 1096 811780 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 8408452 6331344 1628392 80% /
tmpfs 4064380 0 4064380 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1768056 250660 1409264 16% /boot
tmpfs 812876 4 812872 1% /run/user/1000