NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Linux Disk Full? How to Find What's Using Space and Free It Safely

When a disk fills up, things break in odd ways: MySQL crashes or refuses writes, websites show blank pages, uploads fail, SSH logins hang, and services refuse to start. The error is often No space left on device, but sometimes you only see the side effects. This guide shows how to find what is using the space and free it safely — without deleting something you need.

Step 1: Confirm the problem

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G   59G     0 100% /

Note which filesystem is full — /, /var, /home or a data disk. Then check inodes:

df -i

If IUse% is 100% but space is available, the problem is too many small files, not size — see step 6.

Step 2: Find the large directories

Start at the full filesystem and drill down. -x stays on one filesystem:

sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -15
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -15

Or use ncdu, an interactive browser that is much faster for this job:

sudo apt install ncdu      # or: sudo dnf install ncdu
sudo ncdu -x /

Navigate with arrow keys; d deletes (carefully).

Find the largest individual files:

sudo find / -xdev -type f -size +500M -exec ls -lh {} \; 2>/dev/null | sort -k5 -h

Step 3: The usual suspects and how to clean them safely

Logs (/var/log)

sudo du -sh /var/log/* | sort -h | tail
  • A single huge log (an application or web server log) → truncate it rather than delete it if a process has it open:
sudo truncate -s 0 /var/log/nginx/huge-site.access.log
  • Old rotated logs (*.gz, *.1) can be deleted.
  • Then fix rotation so it does not happen again (/etc/logrotate.d/).

systemd journal

journalctl --disk-usage
sudo journalctl --vacuum-size=200M

Make it permanent with SystemMaxUse=500M in /etc/systemd/journald.conf, then sudo systemctl restart systemd-journald.

Package caches and old kernels

# Ubuntu / Debian
sudo apt clean
sudo apt autoremove --purge

# AlmaLinux / Rocky
sudo dnf clean all
sudo dnf remove --oldinstallonly --setopt installonly_limit=2

autoremove removes old kernels that are no longer needed (it keeps the running one and the latest).

Docker

Docker images, stopped containers, build cache and logs can take tens of gigabytes:

docker system df
docker image prune -a
docker builder prune
sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail

Huge *-json.log files are container logs — set log limits as described in installing Docker. Do not run docker system prune --volumes unless you are sure no data lives in unused volumes.

Backups stored on the same disk

Local backup folders (/var/backups, /root/backup, panel backup directories) often grow without cleanup. Delete old ones — and move backups off the server; a backup on the same disk does not protect against disk failure anyway. See restic backups.

Databases

sudo du -sh /var/lib/mysql/* | sort -h | tail
  • Binary logs (mysql-bin.000123, binlog.*) can grow very large. If you do not use replication or point-in-time recovery, set a retention instead of deleting files by hand:
SET GLOBAL binlog_expire_logs_seconds = 259200;   -- 3 days (MariaDB: expire_logs_days on older versions)
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 3 DAY;
  • Large tables: old sessions, logs, cache tables in CMS databases. Clean them through the application where possible.

Never delete files inside /var/lib/mysql directly — it corrupts the database.

Caches and temp files

sudo du -sh /tmp /var/tmp ~/.cache /var/cache/* 2>/dev/null | sort -h | tail

Application caches (WordPress cache plugins, Composer, npm, pip) can usually be cleared through the application or deleted safely when the app is not running.

Mail queue

A spam-sending script can fill /var/spool/postfix with thousands of messages — which also means the server is compromised. See checking IP blacklists and has my server been hacked?.

Step 4: Space not freed after deleting? Open deleted files

If you delete a file that a process still has open, the space is not released until the process closes it.

sudo lsof +L1 | sort -k7 -n | tail
# or
sudo find /proc/*/fd -ls 2>/dev/null | grep '(deleted)'

Restart the process holding the file (often a web server, PHP-FPM or a logging daemon), or truncate the file through its file descriptor:

sudo truncate -s 0 /proc/PID/fd/FD

Step 5: Reserved blocks (ext4)

ext4 reserves 5% of the filesystem for root by default, so df can show 0 available while root can still write. On a large data disk (not /), reducing the reserve can recover space:

sudo tune2fs -m 1 /dev/vdb1

Keep the default on the root filesystem; the reserve helps the system keep running when users fill the disk.

Step 6: Out of inodes

Millions of tiny files — PHP session files, cache fragments, mail queue entries — can exhaust inodes:

sudo find / -xdev -type d -print0 2>/dev/null | xargs -0 -I{} sh -c 'echo "$(ls -A "{}" | wc -l) {}"' | sort -n | tail

That lists directories with the most entries. Common fixes: clean old PHP sessions (/var/lib/php/sessions), purge a runaway cache directory, fix the application creating files without cleanup.

Step 7: Recovering a broken service

After freeing space, restart what failed:

sudo systemctl restart mariadb nginx php8.3-fpm
sudo systemctl --failed

Check database tables if MySQL crashed while the disk was full (sudo mariadb-check --all-databases).

Prevent it happening again

  • Monitoring and alerts at 80% and 90% usage — Netdata has this built in, or use a cron script.
  • Log rotation for every application log; journal size limit.
  • Docker log limits.
  • Backups off the server, with retention.
  • Separate data disk for databases and uploads on servers with more than one disk.
  • Check growth monthly; plan an upgrade or extra storage before you run out.