Sub topic 7
Why Inodes Run Out: Mail Queues, Session Files, and Caches
Assume you can use a shell over SSH, understand files, directories, permissions, and sudo, and have already used df -i to identify a filesystem with high inode usage. This chapter focuses on finding why that filesystem contains too many separate files.
What consumes inodes
An inode is filesystem metadata for one file, directory, symbolic link, or other filesystem object. It records information such as ownership, permissions, timestamps, and the locations of the file's data blocks. A directory also consumes an inode.
A filesystem can therefore have free disk space but no free inodes. For example, millions of 1 KB cache files may consume nearly every inode while using less space than one large video file.
| Workload | Typical inode pattern | First place to inspect |
|---|---|---|
| Mail queue | Many small pending messages | `/var/spool` |
| Login or application sessions | One or more files per session | `/var/lib`, `/run`, or application data |
| Cache | Many disposable small files | `/var/cache` or application cache directories |
| Temporary work | Short-lived files left behind | `/tmp` or `/var/tmp` |
Find directories with many entries
Start at the affected mount point. Counting entries helps distinguish an inode problem from a large-file problem.
for d in /var/*; do
printf '%s %s\n' \
"$(sudo find "$d" -xdev 2>/dev/null | wc -l)" \
"$d"
done | sort -n | tail -n 10The command counts directory entries below each direct child of /var, stays on the same filesystem with -xdev, and shows the largest counts. The count includes directories, so it is an estimate of inode consumption rather than a byte total.
After finding a likely directory, inspect its structure:
sudo find /var/spool -xdev -type f -printf '%h\n' 2>/dev/null |
sort | uniq -c | sort -n | tail -n 10This groups regular files by their parent directory. A deep queue directory or cache subdirectory with an unusually high count deserves investigation.
Common causes
Mail systems store messages in queue directories while they wait for delivery, retry delivery, or await administrator action. A delivery failure, unavailable recipient server, or spam burst can create a large queue. Do not remove queue files manually unless the mail system's documentation provides a supported command. Deleting them can corrupt queue state or discard mail.
Session stores often create files for web logins, PHP sessions, job state, or authentication tokens. A broken expiration job can leave old sessions indefinitely. Before cleanup, confirm the application's session format and retention policy. Removing active session files may log users out, but deleting files that the application is currently writing can cause errors.
Caches are usually safer to remove because software can recreate them. They are still application-specific. Check whether the service has a cache-clear command, and avoid deleting a cache while a program is rebuilding it unless the service supports that operation.
A practical diagnosis path
flowchart TD
A["df -i shows high inode use"] --> B["Count entries below mount"]
B --> C{"Which tree has the most files?"}
C --> D["Inspect mail queues"]
C --> E["Inspect session stores"]
C --> F["Inspect caches and temporary files"]
D --> G["Use service-supported retention or purge"]
E --> H["Fix expiration and retention jobs"]
F --> I["Use cache-specific cleanup"]Look for the process or package that owns the directory:
sudo stat -c '%U:%G %n' /var/spool/example
sudo find /var/spool/example -xdev -type f -printf '%TY-%Tm-%Td %TH:%TM %p\n' |
sort | headOwnership and file age provide evidence about which service created the files and whether they are stale. Correct the cause, then verify with df -i rather than relying only on a directory count.
Exercise
A filesystem has 12% space used but 99% of its inodes used. What should you investigate first, and why?
Show answer
Investigate directories containing many small files, such as mail queues, session stores, caches, and temporary-file trees. Large files are unlikely to explain the inode exhaustion because each file consumes an inode regardless of its size.