Inode Exhaustion Playbook
The disk shows 40% free space, but nothing can write. "No space left on device" errors everywhere. The filesystem has run out of inodes - the metadata entries that track every file and directory. This is one of the most misdiagnosed infrastructure failures because every standard check says "disk is fine."
Operator's Field Notes
Jeremy Martinez · Incident Commander | Principal Site Reliability Engineer
"I've watched teams spend two hours debugging an 'ENOSPC' error while staring at a disk that shows 60% free. They check application logs, they check the database, they redeploy, they restart services. Nobody runs df -i. Every time, the fix takes 10 minutes once you know it's inodes. The diagnosis takes two hours because nobody thinks to check."
"The usual culprits: millions of tiny session files in /tmp, PHP session files that never get cleaned, mail queue directories with millions of bounce messages, Docker overlay2 layers from years of image builds, or Prometheus/metrics directories with one file per metric per scrape. The pattern is always the same: something creates files in a loop, nobody cleans them up, and six months later you have 20 million files and zero free inodes."
What Are Inodes and Why Do They Run Out?
An inode (index node) is a data structure in Unix-like filesystems that stores metadata about a file: permissions, ownership, timestamps, size, and pointers to the actual data blocks. Every file, directory, symbolic link, and device file consumes one inode. The total number of inodes is fixed when the filesystem is created and generally cannot be changed without reformatting.
How Inodes Are Allocated
- • ext4 default: 1 inode per 16KB of disk space. A 100GB disk gets ~6.5 million inodes
- • XFS: Dynamically allocates inodes - rarely runs out (but can still fill up)
- • Btrfs: Dynamic inodes - effectively unlimited
- • tmpfs: Limited by kernel parameters, can fill quickly
Why They Run Out
- • Millions of tiny files (sessions, cache entries, temp files)
- • Each file uses one inode regardless of file size - a 0-byte file and a 1GB file both use 1 inode
- • Mail queues with millions of individual messages
- • Package manager / build caches with one file per artifact
- • Monitoring/metrics systems creating files per metric per interval
KEY INSIGHT
df -h only shows disk space usage.df -i shows inode usage. You must check BOTH when diagnosing "No space left on device" errors. A filesystem at 40% disk usage but 100% inode usage will behave identically to a full disk.
First 5 Minutes: Triage
Confirm inode exhaustion
df -i - Check the IUse% column. If any filesystem shows 100% inode usage, that's your problem. Note which mount point is affected - /, /var, /tmp, and /data are most common.
Find which directories have the most files
find / -xdev -printf '%h\n' | sort | uniq -c | sort -rn | head -20 - This counts files per directory. The top result is your culprit. WARNING: this can be slow on a filesystem with millions of files.
Quick count of total files per top-level dir
for d in /*; do echo -n "$d: "; find "$d" -xdev -type f 2>/dev/null | wc -l; done - Faster overview. Look for directories with millions of files.
Assess blast radius
Which services write to the affected filesystem? Databases, applications, and logging all fail when they can't create files. Same symptoms as disk full - writes fail, services crash.
Emergency cleanup of obvious culprits
Delete files in the largest directory. If it's /tmp session files: find /tmp -type f -mtime +1 -delete. If it's mail queue: find /var/spool/mail -type f -delete. Target the directory with the most files.
Comprehensive Inode Diagnostics
1. Identify Inode Usage
# Inode usage per filesystem df -i # Example output: # Filesystem Inodes IUsed IFree IUse% Mounted on # /dev/sda1 6553600 6553600 0 100% / <-- FULL # /dev/sdb1 1310720 45230 1265490 4% /data # Total inode capacity of a filesystem tune2fs -l /dev/sda1 | grep -i inode # Look for: Inode count, Free inodes # Inodes per directory (fast - first-level only) for d in /*; do printf "%-30s %s\n" "$d" "$(find "$d" -xdev -maxdepth 1 -type f 2>/dev/null | wc -l)" done | sort -t' ' -k2 -rn | head -20
2. Find the Offending Directory
# Count files per directory (recursive) - find the hot spot
# WARNING: Can be slow on millions of files. Start with maxdepth.
# Level 1 - broad overview
find / -xdev -maxdepth 2 -type d -exec sh -c 'echo "$(find "$1" -maxdepth 1 -type f | wc -l) $1"' _ {} \; 2>/dev/null | sort -rn | head -20
# Once you find the suspect directory, drill deeper:
find /var/spool -maxdepth 3 -type d -exec sh -c 'echo "$(find "$1" -maxdepth 1 -type f | wc -l) $1"' _ {} \; 2>/dev/null | sort -rn | head -20
# Fastest method - count by parent directory hash table
# (handles millions of files efficiently)
find / -xdev -printf '%h\n' 2>/dev/null | sort | uniq -c | sort -rn | head -30
# Check specific known problem directories
echo "Sessions: $(find /tmp -maxdepth 1 -type f 2>/dev/null | wc -l)"
echo "Mail: $(find /var/spool -type f 2>/dev/null | wc -l)"
echo "Cache: $(find /var/cache -type f 2>/dev/null | wc -l)"
echo "Log: $(find /var/log -type f 2>/dev/null | wc -l)"
echo "Docker: $(find /var/lib/docker -maxdepth 3 -type f 2>/dev/null | wc -l)"
echo "Packages: $(find /var/lib/dpkg -type f 2>/dev/null | wc -l)"
# File age distribution in suspected directory
find /tmp -type f -printf '%T+ %p\n' 2>/dev/null | cut -d' ' -f1 | cut -dT -f1 | sort | uniq -c | sort -rn | head -10Tip: If ls hangs on a directory, it likely has millions of files. Use ls -U (unsorted) which doesn't load the entire directory into memory, or find with -maxdepth 1.
3. Safe Mass Deletion of Millions of Files
# IMPORTANT: rm -rf on a directory with millions of files will: # 1. Take a very long time # 2. May cause I/O storms that affect other services # 3. May appear to hang (it's working, just slowly) # Method 1: find + delete (most controlled) # Delete files older than 7 days, 10000 at a time find /tmp -type f -mtime +7 -print0 | xargs -0 -n 10000 rm -f # Method 2: Parallel deletion with rate limiting find /tmp -type f -mtime +1 -print0 | xargs -0 -P 4 -n 5000 rm -f # Method 3: rsync trick (fastest for emptying a directory) mkdir /tmp/empty_dir rsync -a --delete /tmp/empty_dir/ /path/to/dir-with-millions/ rmdir /path/to/dir-with-millions # Method 4: For EXTREME cases (tens of millions of files) # Create a new filesystem, mount it, move the good files, swap # This is faster than deleting the bad files # Monitor progress during deletion watch -n 5 'df -i / | tail -1' # Throttle deletion to avoid I/O storms find /problem/dir -type f -mtime +1 -print0 | \ xargs -0 -n 1000 sh -c 'rm -f "$@"; sleep 1' _
Critical: Never use rm -rf /path/* on a directory with millions of files - the shell glob expansion will consume all available memory trying to expand the wildcard. Always use find + xargs.
Common Inode Exhaustion Culprits
PHP/Application Session Files
/tmp or /var/lib/php/sessions
PHP creates one file per session by default. On high-traffic sites, this creates millions of tiny files. Sessions that are never cleaned up accumulate indefinitely. Fix: Configure session.gc_maxlifetime, switch to Redis/Memcached sessions, or add a cron to clean old sessions.
Quick fix: find /var/lib/php/sessions -type f -mtime +1 -delete
Mail Queue / Bounce Messages
/var/spool/mail, /var/spool/postfix
Mail servers store one file per message. If a server is sending notifications and the recipient is invalid, bounce messages accumulate forever. On servers with misconfigured cron mail, every failed cron creates a mail file.
Quick fix: find /var/spool/postfix/deferred -type f -mtime +7 -delete
Container Image Layers (Docker/containerd)
/var/lib/docker/overlay2
Every Docker image layer creates directories and files. Frequent builds, pulls, and deploys without pruning leads to hundreds of thousands of overlay2 directories. Each layer has multiple files for the diff, merged, and work directories.
Quick fix: docker system prune -a --volumes
Metrics / Monitoring Files
/var/lib/prometheus, /var/lib/node_exporter
Some monitoring agents create one file per metric per scrape interval. With thousands of metrics scraped every 15 seconds, this can create millions of files per day.
Quick fix: Check your metrics agent configuration for file-based storage and switch to push-based.
Log Rotation Gone Wrong
/var/log
Misconfigured logrotate that rotates into numbered files without removing old ones. Can create thousands of .log.1, .log.2, etc. files. Also: applications that create a new log file per request or per transaction.
Quick fix: find /var/log -name '*.gz' -mtime +30 -delete
Temp Files and Cache Directories
/tmp, /var/tmp, /var/cache
Applications creating temporary files without cleanup. Build artifacts, test outputs, pip/npm/gem caches, and failed upload temp files all accumulate.
Quick fix: find /tmp -type f -atime +7 -delete && find /var/cache -type f -mtime +30 -delete
Git Repositories (Bare/Build)
/var/lib/jenkins, /home/git
Git pack files, loose objects, and worktree files. CI/CD servers with many builds accumulate git objects. Each git clone creates thousands of files in .git/objects.
Quick fix: git gc --aggressive && git prune
Kubernetes Secrets/ConfigMaps (tmpfs)
Pod tmpfs mounts
In Kubernetes, each Secret and ConfigMap mounted as a volume creates files in tmpfs. Pods with hundreds of mounted configs can exhaust tmpfs inodes, which are separate from disk inodes.
Quick fix: Reduce the number of mounted secrets or use environment variables instead.
Platform-Specific Considerations
AWS (EBS)
EBS volumes formatted with ext4 have a fixed inode count set at format time. Expanding the EBS volume does NOT increase the inode count - only the disk space. If you're inode-exhausted, expanding the volume won't help. You need to either clean files or reformat with more inodes (destructive).
# Check inode count on EBS ext4 volume tune2fs -l /dev/nvme1n1p1 | grep -i "inode count" # If you must reformat with more inodes: # 1. Create a new, larger EBS volume # 2. Format with more inodes: mkfs.ext4 -i 4096 /dev/nvme2n1 # (-i 4096 means 1 inode per 4KB instead of default 16KB = 4x more inodes) # 3. Copy data: rsync -avHAX /old-mount/ /new-mount/ # 4. Swap mount points # Consider XFS for new volumes - it dynamically allocates inodes mkfs.xfs /dev/nvme2n1
Azure Managed Disks
Same ext4 inode limitation applies. Azure's temp disk (typically /mnt or /mnt/resource on Linux) is formatted with a default inode count that may be insufficient for workloads creating many small files. Azure Monitoring does not track inode usage by default - you need a custom metric.
# Check Azure disk inode usage df -i /datadrive # Custom Azure Monitor metric for inode tracking # Use the Azure Linux Agent custom metrics or telegraf: # [[inputs.disk]] # mount_points = ["/", "/datadrive"] # This reports inodes_total, inodes_free, inodes_used
GCP Persistent Disk
GCP recommends XFS for Persistent Disks, which dynamically allocates inodes. If you're using ext4 on GCP, the same fixed-inode issue applies. GCP's Operations Suite (Stackdriver) does not track inodes by default.
# If using ext4 on GCP - check and consider migrating to XFS cat /etc/fstab | grep -v "^#" # If ext4, consider reformatting data volumes as XFS for dynamic inodes # GCP Ops Agent custom metric for inodes # In the agent config, the disk input plugin tracks inodes by default
Kubernetes
Kubernetes nodes can hit inode exhaustion from container image layers and container log files. Kubelet monitors inode usage and will evict pods when nodefs or imagefs inodes are critically low. The default eviction threshold is when free inodes drop below 5%.
# Check if kubelet is evicting pods due to inode pressure kubectl get events --all-namespaces | grep -i "inode\|DiskPressure" kubectl describe node <node> | grep -A 3 "Conditions" | grep -i disk # Node inode usage from inside ssh <node-ip> "df -i /var/lib/docker && df -i /var/lib/kubelet" # containerd/Docker image cleanup to free inodes crictl rmi --prune docker image prune -a # Set kubelet eviction thresholds for inodes # In kubelet config: # evictionHard: # nodefs.inodesFree: "5%" # imagefs.inodesFree: "5%"
Long-Term Fixes and Prevention
Filesystem Choices
- Use XFS for data volumes where many small files are expected - it dynamically allocates inodes and won't hit this problem
- Format ext4 with
-i 4096to get 4x more inodes (1 per 4KB instead of default 1 per 16KB) - Use Btrfs where supported - unlimited dynamic inodes
- Separate partitions for /tmp, /var/log, and data dirs so inode exhaustion in one doesn't affect others
Application Architecture
- Store sessions in Redis/Memcached instead of filesystem - eliminates the #1 cause of inode exhaustion
- Use object storage (S3/GCS/Blob) for file uploads instead of local disk
- Aggregate metrics into fewer files instead of one file per metric
- Use database-backed caches instead of file-based caching
Monitoring & Alerting
# Prometheus node_exporter tracks inodes automatically:
# node_filesystem_files_free{mountpoint="/"}
# node_filesystem_files{mountpoint="/"}
#
# Alert rule:
# - alert: InodeExhaustionWarning
# expr: node_filesystem_files_free / node_filesystem_files < 0.15
# for: 10m
# labels:
# severity: warning
# annotations:
# summary: "Filesystem {{ $labels.mountpoint }} has < 15% free inodes"
#
# - alert: InodeExhaustionCritical
# expr: node_filesystem_files_free / node_filesystem_files < 0.05
# for: 5m
# labels:
# severity: critical
# Cron-based inode monitoring (if no Prometheus)
# Add to /etc/cron.d/inode-check:
*/5 * * * * root df -i / | awk 'NR==2{gsub(/%/,""); if($5>90) system("logger -p local0.crit INODE_WARNING: / at "$5"% inode usage")}'Post-Incident Questions
What created the millions of files? Is it a known application pattern or a bug?
Why wasn't there monitoring on inode usage? Can we add alerts for all production filesystems?
Should we switch to XFS or format ext4 with more inodes for this use case?
Can the application be changed to use fewer files (Redis sessions, S3 uploads, aggregated metrics)?
Is there automated cleanup in place? Does it run frequently enough?
Should this directory be on a separate partition to isolate the blast radius?
Did the monitoring gap exist on other hosts too? How many servers are at risk?