A laptop on a dark desk with a glowing terminal window out of focus — Linux Disk Cleanup Commands for Safer Space Recovery

Linux Disk Cleanup Commands for Safer Space Recovery

Spread the love

A full root partition rarely announces itself politely. The desktop drops you back to the login screen, sudo refuses to start because it cannot write a timestamp file, and every command returns “No space left on device” even though you deleted 4 GB five minutes ago. The temptation is to start removing folders with names you do not recognise. Measure first: on a 256 GB laptop SSD the difference between the real hog and the folder that merely looks large is usually one du run away. The commands below target Debian, Ubuntu and Linux Mint with apt; Fedora and Arch equivalents are noted where they differ.

Read df and du before you delete anything

df -h shows usage per mounted filesystem, and the Use% column is what matters, not the size of the disk. If / is at 96% and /home sits on the same filesystem, deleting from your Downloads folder is the only thing that will help. Run df -i in the same breath: it reports inode usage, and a filesystem can be 40% empty by bytes while being completely out of inodes.

To find the directory responsible, drill down one level at a time rather than scanning everything:

  • sudo du -xh --max-depth=1 / | sort -h
  • sudo du -xh --max-depth=1 /var | sort -h
  • sudo du -xh --max-depth=1 /home/yourname | sort -h

The -x flag stays on one filesystem, which matters if you have a NAS mount or an external drive plugged in — without it, du / will sit there reading network shares for twenty minutes. ncdu -x / does the same job interactively and lets you delete with d, which is faster than typing paths but easier to regret; the du(1) manual page documents the flags worth knowing, including --exclude for skipping a backup folder.

Caches and logs that grow back on their own

These are the directories that refill after every cleanup, so it helps to know their ceiling. ~/.cache holds browser caches, thumbnail previews, pip wheels and Flatpak runtimes; deleting the whole folder is safe, and applications rebuild what they need — the profile-level detail is in browser cache cleanup without breaking your logins. sudo apt clean empties /var/cache/apt/archives, which commonly holds 1–3 GB of .deb files on a machine that has been updated for a year; apt autoclean removes only packages no longer downloadable.

systemd’s journal is the other silent grower. Check it with journalctl --disk-usage, trim it with sudo journalctl --vacuum-size=200M or --vacuum-time=14d, then make the cap permanent by setting SystemMaxUse=200M under [Journal] in /etc/systemd/journald.conf and running sudo systemctl restart systemd-journald. Snap keeps two revisions of every installed package by default: snap list --all | grep disabled lists the dead ones, sudo snap set system refresh.retain=2 prevents three from piling up, and flatpak uninstall --unused clears orphaned runtimes.

A laptop on a dark desk with a glowing terminal window out of focus — Linux Disk Cleanup Commands for Safer Space Recovery

Large files, and the paths you must not touch

A single 12 GB file is easier to deal with than ten thousand small ones. This lists everything over 500 MB on the root filesystem, largest last:

sudo find / -xdev -type f -size +500M -printf '%st%pn' 2>/dev/null | sort -n | tail -20

In practice the culprits are predictable: an Ubuntu ISO left in ~/Downloads, Timeshift snapshots in /timeshift, virtual machine images in /var/lib/libvirt/images, crash dumps in /var/crash that can each be a gigabyte, and old kernels. dpkg --list | grep linux-image shows them; sudo apt autoremove --purge removes all but the running one. Leave bare the contents of /usr, /lib, /var/lib/dpkg and /boot/efi — deleting from those does not free meaningful space and does break the package database.

When inodes run out instead of bytes

If df -h shows free space but writes still fail, look at df -i. Mail queues, PHP session files and package caches create millions of tiny files. /var/spool/postfix grows without bound on a machine whose cron mail is bouncing, /var/lib/php/sessions collects one file per visitor, and ~/.npm/_cacache can reach 300,000 entries. Count files per directory before deleting:

for d in /var/*; do printf '%s %sn' "$(find "$d" -xdev 2>/dev/null | wc -l)" "$d"; done | sort -n | tail

Prove the system still boots before you reboot

Two checks catch most self-inflicted damage. First, half-configured packages: dpkg -l | grep -v '^ii' lists anything not fully installed, and sudo apt -f install plus sudo dpkg --configure -a repairs it. Second, the bootloader: after removing kernels, run sudo update-grub so the menu does not point at a deleted image.

ext4 reserves 5% of the volume for root, which is why a disk at 100% can still let root log in. Check with sudo tune2fs -l /dev/nvme0n1p2 | grep -i 'reserved block'; lowering it with tune2fs -m 1 frees space on a data disk but should not be used to squeeze the last gigabytes out of /. Aim to finish with 10–15% free: below that, ext4 fragments badly and btrfs needs room to relocate extents. If you keep Timeshift or btrfs snapshots, take one before the cleanup and delete it a week later once the machine has booted cleanly. On a dual-boot machine the Windows side usually holds the biggest single win, and the folders to clear there are listed in freeing up disk space on Windows 11.

Scroll to Top