Running out of space on a Linux system can block package upgrades, interrupt logging, and cause services to fail when they cannot write data. Keeping enough free space on the main file systems preserves system stability and reduces the risk of sudden interruptions during critical operations.
Disk usage on Linux is determined by how files are laid out on mounted file systems such as / , /home, and /var. Tools like df, du, and find report which mount points, directories, and individual files consume the most space, while package managers store downloaded packages and metadata under locations such as /var/cache and /var/lib.
Removing files with elevated privileges can permanently delete configuration or application data if the wrong paths are targeted. The procedure below focuses on common safe areas such as package caches, logs, and user data on systemd-based distributions like Ubuntu and Debian, with notes for other distributions. Administrative privileges via sudo are required for system-wide operations, and command lines should be double‑checked before execution.
Steps to clear disk space on Linux:
- Display disk usage for all mounted file systems.
$ df -h Filesystem Size Used Avail Use% Mounted on overlay 59G 21G 35G 38% / tmpfs 64M 0 64M 0% /dev shm 64M 0 64M 0% /dev/shm /dev/vda1 59G 21G 35G 38% /etc/hosts tmpfs 2.4G 456K 2.4G 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock /dev/loop0 974M 24K 907M 1% /mnt/data
Focus on entries where Use% is close to 100% to choose a cleanup target.
- Measure space usage of top-level directories on the most full file system, typically / .
$ sudo du -h -x --max-depth=1 / 4.0K /srv 28M /var 4.0K /boot 56K /home 2.5M /etc 4.0K /opt 8.0K /mnt 703M /usr 50M /root 28K /tmp 4.0K /media 4.0K /bin.usr-is-merged 4.0K /sbin.usr-is-merged 4.0K /lib.usr-is-merged 782M /
The -x option keeps du on a single file system so usage from other mounted disks does not distort results.
- Inspect space usage inside the heaviest system directory, such as /var.
$ sudo du -h --max-depth=1 /var | sort -h 4.0K /var/backups 4.0K /var/local 4.0K /var/mail 4.0K /var/opt 12K /var/tmp 16K /var/spool 1.9M /var/cache 9.6M /var/log 16M /var/lib 28M /var
Subdirectories like /var/log, /var/cache, and /var/tmp often contain data that can be pruned safely when not needed.
- Locate individual files larger than 500MB on the selected file system.
$ sudo find / -xdev -type f -size +500M -printf '%p %s\n' | sort -h /root/Downloads/cleanup-demo.iso 629145600 /root/sg-work/disks/data.img 1073741824 /root/sg-work/disks/mountdemo.img 536870912 /var/tmp/data.img 1073741824 /var/tmp/mount.img 536870912
Before deleting any large file, confirm that it is not part of an active database, virtual machine, or critical application.
- Clear cached package files managed by apt.
$ sudo apt-get clean
On Fedora or other dnf based systems, use sudo dnf clean all instead of apt.
- Remove packages that were installed as dependencies but are no longer required.
$ sudo apt-get autoremove --purge -y Reading package lists... Building dependency tree... Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Review the list shown by apt and abort with Ctrl+C if important packages appear unexpectedly.
Removing packages with autoremove can uninstall tools that are still in occasional use, which may break scripts or workflows until they are reinstalled.
- Reduce journal size by removing entries older than seven days.
$ sudo journalctl --vacuum-time=7d Vacuuming done, freed 0B of archived journals from /var/log/journal/b7197b121c7b43be81a7c8a9fd9a6ce0. Vacuuming done, freed 0B of archived journals from /run/log/journal. Vacuuming done, freed 0B of archived journals from /var/log/journal.
journalctl manages binary logs under /var/log/journal on systemd systems and can limit usage by time or size.
Discarding older logs makes post‑incident analysis harder if issues occurred before the retention window.
- Delete unneeded files from the current user's /Downloads directory and desktop trash.
$ rm -rf ~/Downloads/* $ rm -rf ~/.local/share/Trash/files/*
Temporary installers, archives, and media often accumulate in these locations after they are no longer required.
The rm -rf command permanently removes files without a confirmation prompt, so incorrect paths can erase important data.
- Purge cached data in the current user's /.cache directory.
$ du -sh ~/.cache 4.0K /root/.cache $ rm -rf ~/.cache/* $ du -sh ~/.cache 4.0K /root/.cache
Most applications store only regenerable data in .cache, so space is reclaimed at the cost of slightly slower first launches while caches are rebuilt.
Clearing caches may log out web sessions or remove thumbnails and temporary state for graphical applications.
- Remove home directories that belong to user accounts that no longer exist.
$ sudo ls /home appuser backupuser opsuser ubuntu $ sudo rm -rf /home/appuser
Confirm that the corresponding account has been removed, for example with getent passwd appuser, before deleting its home directory.
Using rm -rf on the wrong path under /home can erase active user data irreversibly.
- Confirm reclaimed disk space on the cleaned file system.
$ df -h Filesystem Size Used Avail Use% Mounted on overlay 59G 21G 35G 38% / tmpfs 64M 0 64M 0% /dev shm 64M 0 64M 0% /dev/shm /dev/vda1 59G 21G 35G 38% /etc/hosts tmpfs 2.4G 456K 2.4G 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock /dev/loop0 974M 24K 907M 1% /mnt/data
Compare the Use% and available space values with those observed in the first step to verify that cleanup had the intended effect.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
