Home FixHow to Fix ‘No Space Left on Device’ Error on Linux

How to Fix ‘No Space Left on Device’ Error on Linux

by Knowledge Base Linux
How to Fix No Space Left — How to Fix 'No Space Left on Device' Error on Linux

The no space left on device error stops everything: websites return 500 errors, databases crash, and even SSH sessions misbehave because the system cannot write session files. Sometimes the fix is obvious — a full disk. But sometimes df insists there is free space while applications still fail. This guide covers both situations: how to find what is really filling your disk, free space safely, and handle the two hidden causes — inodes and deleted-but-open files.

Step 1: Confirm the Real State of the Disk – How to Fix No Space Left

Start with the two commands experienced admins always run together:

df -h
df -i

df -h shows space usage per filesystem; df -i shows inode usage. Every file consumes one inode, and a filesystem can run out of inodes (millions of tiny files, cache files or session files) while gigabytes remain free — the error message is identical. Whichever column shows 100% tells you what to hunt. For a refresher on reading disk usage, see our guide on how to check disk space on Linux.

Cleaning logs and caches to resolve the no space left on device error

Step 2: Find the Largest Directories – How to Fix No Space Left

Drill down from the root of the full filesystem, level by level:

sudo du -xhd1 / | sort -hr | head -20
sudo du -xhd1 /var | sort -hr | head -20

The usual suspects are /var/log (unrotated logs), /var/lib/mysql (binary logs), /var/backups, /home (old backups and uploads) and /tmp. To find individual large files anywhere:

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

Step 3: Clear Logs the Safe Way

Systemd journals and application logs are the most common space hogs. Check and shrink the journal first:

journalctl --disk-usage
sudo journalctl --vacuum-time=7d
sudo journalctl --vacuum-size=500M

Our dedicated tutorial on how to clear the journal log covers making these limits permanent in journald.conf. For plain log files, truncate instead of deleting — deleting a log file that a service still holds open frees nothing (see Step 5):

sudo truncate -s 0 /var/log/huge-app.log

Then check why rotation failed and confirm logrotate runs daily.

Step 4: Clean Caches and Package Leftovers

Package caches and old kernels accumulate silently:

sudo apt autoremove --purge        # Ubuntu/Debian
sudo apt clean
sudo dnf autoremove                 # RHEL/AlmaLinux
sudo dnf clean all

Also inspect Docker if installed — unused images and stopped containers eat tens of gigabytes: docker system df shows the breakdown and docker system prune reclaims it. Old website backups stored inside home directories are another frequent find; move them off-server instead of deleting your only copies.

Step 5: Hunt Deleted-but-Open Files

If df says the disk is full but du cannot find the data, a process is holding a deleted file open — the space returns only when the process exits. Find them with:

sudo lsof +L1 | grep deleted

The classic case is a service whose multi-gigabyte log was deleted instead of truncated. Restarting the listed service releases the space immediately. This is why Step 3 uses truncate for open logs.

Step 6: Fix Inode Exhaustion

If df -i showed 100%, find the directory with the most files:

for d in /var/*; do echo -n "$d: "; sudo find "$d" -type f 2>/dev/null | wc -l; done

Typical sources: PHP session files in /var/lib/php/sessions, mail queues, cache directories of CMS plugins, and millions of small files from a misbehaving cron job. Delete the stale files and fix the job that created them so it does not refill the filesystem.

How to Prevent ‘No Space Left on Device’

Set journald size limits, verify logrotate works, alert at 80% disk and inode usage, and keep backups on separate storage. A monitoring stack such as Zabbix can graph both metrics so you see the trend weeks before the disk fills. The behavior of df, inodes and filesystems is documented in the GNU coreutils manual.

Emergency First Aid When the Disk Is Already Full

A completely full root filesystem can block the very tools you need — package managers, editors saving config files, even new SSH sessions on some setups. Work in this order: first truncate (do not delete) the biggest open log you found; second, clear the journal with journalctl --vacuum-size=200M; third, drop package caches. That usually frees enough to breathe. Avoid the temptation to delete database files, mail spools or “mysterious” directories under /var/lib — on a full disk you are one wrong rm -rf away from data loss, and almost everything under /var/lib is data, not waste. If the server hosts websites, also check for core dumps in site directories (find /home -name "core.*" -size +10M) from crashed PHP processes — they are pure waste and often total gigabytes. Once space is recovered, find the writer responsible before it refills the disk overnight.

Conclusion

The no space left on device error is either a genuinely full disk, exhausted inodes, or space trapped by deleted-but-open files. Measure with df -h and df -i first, clean logs and caches safely, restart any process holding deleted files — and put monitoring in place so the next fill-up warns you before it breaks the server.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.