When df shows plenty of free space but you still get 'no space left on device', you have run out of inodes, not disk blocks. This guide shows how to diagnose, clean up, and prevent inode exhaustion on Linux.
Prerequisites
- Root or sudo access to the server
- Basic familiarity with the Linux command line
- A server showing ENOSPC or 'No space left on device' errors
What this solves
You get 'No space left on device' errors when writing files, but df -h shows gigabytes of free disk space. This happens when a filesystem runs out of inodes, the metadata structures that track every file and directory, even though raw block storage is still available.
This tutorial covers how to diagnose inode exhaustion, find the directories causing it, clean up safely, and prevent it from happening again.
Step-by-step diagnosis and fix
Understand inodes vs disk blocks
Every file and directory on a Linux filesystem consumes exactly one inode, regardless of its size. A filesystem is formatted with a fixed number of inodes at creation time, usually based on an assumed average file size.
If your workload creates millions of tiny files, like session files, cache entries, or mail queue items, you can exhaust the inode table while the actual disk blocks remain mostly empty. Once inodes run out, the kernel refuses to create any new file, even a 1-byte one, and returns ENOSPC, which shows up as 'No space left on device'.
Confirm inode exhaustion with df -i
The standard df -h command only reports block usage. Use the -i flag to check inode usage per filesystem.
df -iLook at the IUse% column. If it is at or near 100% on the filesystem that is failing writes, you have confirmed inode exhaustion rather than a space problem.
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 1310720 1310698 22 100% /Compare this against block usage to see the mismatch.
df -h /If df -h shows 40% used but df -i shows 99-100% used, that is a clear inode exhaustion signature. For a deeper refresher on these commands, see check disk space usage on Linux with df and du commands.
Find directories with the most files
You need to locate which directory tree is consuming the inodes. Counting files per top-level directory narrows down the search quickly.
for dir in /var /tmp /home /usr /opt; do echo "$dir: $(find "$dir" -xdev | wc -l)"; doneOnce you identify the heaviest top-level directory, drill down further.
sudo find /var -xdev -maxdepth 3 -type d -exec sh -c 'echo $(find "$1" -maxdepth 1 | wc -l) "$1"' _ {} \; | sort -rn | head -20This lists the directories with the highest file counts, sorted descending. The top entries are almost always your culprit.
Identify the common culprits
Most inode exhaustion incidents trace back to a small set of recurring sources. Check these first before doing a broader scan.
| Location | Typical cause |
|---|---|
/var/lib/php/sessions | PHP session files never cleaned up (cron or garbage collection disabled) |
/var/spool/mail and /var/spool/postfix | Mail queue backlog or bounce loops generating queue files |
/var/cache and application cache dirs | Cache frameworks writing one file per key with no eviction |
/var/log | Applications logging to per-request or per-session files instead of rotating logs |
/tmp and /var/tmp | Runaway scripts or build tools leaving temp files behind |
| Application upload directories | Thumbnail generation or per-upload metadata files that are never pruned |
You can check specific suspects directly.
sudo find /var/lib/php/sessions -type f | wc -l
sudo find /var/spool/postfix/maildrop -type f | wc -l
sudo find /tmp -type f | wc -lList and inspect the offending files
Once you have narrowed the search to a directory, list a sample of the files to confirm what is generating them before deleting anything.
ls -la /var/lib/php/sessions | head -20Check file ages to understand if this is an active leak or old accumulated cruft.
sudo find /var/lib/php/sessions -type f -mtime +7 | wc -lThis tells you how many files are older than 7 days, which are typically safe to remove.
Clean up safely to reclaim inodes
Delete only files you have verified are stale or regenerable. Never run a blanket delete on directories you have not inspected.
sudo find /var/lib/php/sessions -type f -mtime +1 -deleteFor large numbers of files, rm -rf on a directory with millions of entries can be slow and may itself fail if the shell tries to glob-expand the file list. Use find -delete or find | xargs rm -f instead, since they stream file names rather than expanding them all at once.
sudo find /tmp -type f -mtime +3 -deleteFor mail spool cleanup, check and clear the Postfix queue carefully rather than deleting files directly.
mailq | wc -l
sudo postsuper -d ALL deferredchmod 777 on these directories to work around permission errors during cleanup. If deletion fails with permission denied, use sudo for the specific command, or fix ownership with chown on the actual directory the application user needs to write to. Loosening permissions to 777 does not fix inode exhaustion and exposes the directory to every local user on the system.Verify inodes were reclaimed
After cleanup, recheck inode usage to confirm you have headroom again.
df -i /If IUse% is still high, repeat the directory scan from step 2, since there may be a second source you have not found yet.
Prevent recurrence with log rotation
If log files or per-session files are the root cause, configure logrotate or an application-level cleanup job rather than relying on manual intervention.
/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
maxsize 50M
}For PHP session cleanup, verify that garbage collection is actually enabled rather than disabled by a misconfigured php.ini.
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440Restart PHP-FPM after changing this.
sudo systemctl restart php8.3-fpmSee install and configure Nginx with PHP 8.4 for beginners for a full PHP-FPM setup reference.
Move high-churn directories to tmpfs
Session files and short-lived cache data are good candidates for tmpfs, a RAM-backed filesystem that does not consume disk inodes at all and is automatically cleared on reboot.
tmpfs /var/lib/php/sessions tmpfs defaults,noatime,size=256M,uid=www-data,gid=www-data,mode=1733 0 0Apply it without rebooting.
sudo mount /var/lib/php/sessionsAdd monitoring so you catch it early
Set up a simple cron-based check or integrate inode usage into your existing monitoring stack so you get alerted before inodes hit 100%.
df -i / | awk 'NR==2 {gsub("%","",$5); if ($5+0 > 85) print "WARNING: inode usage at " $5 "%"}'Wrap this in a cron job that emails or pages you, or feed it into Prometheus with the node_exporter filesystem collector, which already exposes node_filesystem_files and node_filesystem_files_free metrics. See set up Prometheus and Grafana monitoring stack with Docker Compose to wire up alerting on these metrics.
When to resize or reformat with more inodes
If your workload legitimately needs millions of small files, for example a mail server or a cache with high churn, the permanent fix is reformatting with a higher inode density rather than fighting cleanup forever.
For ext4, you control inode count at format time with the bytes-per-inode ratio. A smaller ratio allocates more inodes relative to disk size.
sudo mkfs.ext4 -i 4096 /dev/sdb1This allocates one inode per 4096 bytes instead of the default 16384, roughly quadrupling the inode count for the same disk size. This only applies when formatting a new filesystem or partition, it cannot be changed on a live, already-formatted filesystem without backing up data, reformatting, and restoring.
XFS handles this more gracefully since it allocates inodes dynamically by default and rarely runs into this problem, which makes it a better choice for inode-heavy workloads like mail servers or object caches.
rsync or tar before reformatting, and verify the backup is restorable before proceeding.Verify your setup
df -i
df -h
touch /tmp/inode-test.txt && echo "write succeeded" && rm /tmp/inode-test.txtConfirm IUse% has dropped well below 90% and that you can create new files without error.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| df -h shows free space but writes fail | Inode table is full | Run df -i to confirm, then clean up small-file directories |
| find -delete is very slow | Millions of files in a single flat directory | Let it run in the background with nohup, or delete in batches by modification time |
| Inode usage returns to 100% quickly after cleanup | A process is actively generating files faster than you can delete them | Stop or fix the offending service first, then clean up |
| rm -rf fails with 'Argument list too long' | Shell glob expansion exceeds the argument limit | Use find /path -type f -delete instead of rm -rf /path/* |
| Reformatted filesystem still runs low on inodes | bytes-per-inode ratio still too high for the workload | Lower the ratio with mkfs.ext4 -i 2048 or switch to XFS |
Next steps
- Check disk space usage on Linux with df and du commands
- Configure Linux log rotation with logrotate and compression for system maintenance
- Configure Linux disk usage monitoring and automated cleanup with systemd timers
- Set up RAID arrays with mdadm for performance and redundancy