The Problem
You run df -h and see plenty of free space, yet commands fail with "No space left on device." This is almost always an inode exhaustion issue. Every file and directory on a Linux filesystem consumes one inode, and the total number of inodes is fixed when the filesystem is created.
Diagnosing Inode Exhaustion
# Check inode usage
df -i
# Example output:
# Filesystem Inodes IUsed IFree IUse% Mounted on
# /dev/sda1 655360 655360 0 100% /
If IUse% is 100%, the filesystem has run out of inodes even though bytes are available.
Finding the Culprit
# Count files per directory (top offenders)
for dir in /*; do
echo "$(find "$dir" -xdev 2>/dev/null | wc -l) $dir"
done | sort -rn | head -10
# Drill deeper into the top directory
for dir in /var/*; do
echo "$(find "$dir" -xdev 2>/dev/null | wc -l) $dir"
done | sort -rn | head -10
Common Causes
1. Session Files
PHP session files accumulating without garbage collection:
# Check session directory
ls /tmp/sess_* 2>/dev/null | wc -l
ls /var/lib/php/sessions/ 2>/dev/null | wc -l
# Clean old session files (older than 24 hours)
find /var/lib/php/sessions/ -type f -mtime +1 -delete
find /tmp -name "sess_*" -type f -mtime +1 -delete
2. Mail Queue
Undelivered emails accumulating in the mail queue:
# Check mail queue size
ls /var/spool/postfix/deferred/ | wc -l
ls /var/spool/postfix/maildrop/ | wc -l
# Flush and delete the queue
sudo postsuper -d ALL
3. Cache or Temp Files
# Check /tmp
find /tmp -type f | wc -l
# Check application cache directories
find /var/cache -type f | wc -l
# Clean old temp files
find /tmp -type f -atime +7 -delete
4. Tiny Log Files or PID Files
# Check for excessive small files in /var/log
find /var/log -type f | wc -l
# Check /var/run
find /var/run -type f | wc -l
Fixing the Problem
# Once you identify the directory, delete unnecessary files
find /path/to/offending/dir -type f -mtime +7 -delete
# For massive file counts, use xargs for efficiency
find /path/to/dir -type f -mtime +7 -print0 | xargs -0 rm -f
# Verify inodes are freed
df -i
Prevention
- Set up log rotation with
logrotate - Configure PHP session garbage collection:
session.gc_maxlifetimeandsession.gc_probability - Use
tmpreaperortmpwatchto clean/tmpautomatically - Monitor inode usage in your alerting system alongside disk space
- For filesystems needing many small files, create the filesystem with more inodes:
mkfs.ext4 -N 2000000 /dev/sdb1
Cannot Increase Inodes on Existing Filesystem
Unlike disk space, you cannot add more inodes to an existing ext4 filesystem. If you chronically run out, you must back up the data, recreate the filesystem with a higher inode count, and restore.