Fix disk full when df shows free space (inode exhaustion)

Beginner 20 min Dec 05, 2026
Ubuntu 24.04 Ubuntu 22.04 Debian 12 Debian 11 AlmaLinux 9 Rocky Linux 9 Fedora 41

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 -i

Look 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)"; done

Once 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 -20

This 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.

LocationTypical cause
/var/lib/php/sessionsPHP session files never cleaned up (cron or garbage collection disabled)
/var/spool/mail and /var/spool/postfixMail queue backlog or bounce loops generating queue files
/var/cache and application cache dirsCache frameworks writing one file per key with no eviction
/var/logApplications logging to per-request or per-session files instead of rotating logs
/tmp and /var/tmpRunaway scripts or build tools leaving temp files behind
Application upload directoriesThumbnail 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 -l

List 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 -20

Check 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 -l

This 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 -delete

For 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 -delete

For mail spool cleanup, check and clear the Postfix queue carefully rather than deleting files directly.

mailq | wc -l
sudo postsuper -d ALL deferred
Warning: never run chmod 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 = 1440

Restart PHP-FPM after changing this.

sudo systemctl restart php8.3-fpm

See 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 0

Apply it without rebooting.

sudo mount /var/lib/php/sessions
Note: tmpfs content is lost on reboot. Only use it for data that is safe to lose, such as sessions, not for anything requiring persistence like uploaded files or queued jobs.

Add 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/sdb1

This 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.

Warning: reformatting destroys all data on the partition. Always take a full backup with 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.txt

Confirm IUse% has dropped well below 90% and that you can create new files without error.

Common issues

SymptomCauseFix
df -h shows free space but writes failInode table is fullRun df -i to confirm, then clean up small-file directories
find -delete is very slowMillions of files in a single flat directoryLet it run in the background with nohup, or delete in batches by modification time
Inode usage returns to 100% quickly after cleanupA process is actively generating files faster than you can delete themStop or fix the offending service first, then clean up
rm -rf fails with 'Argument list too long'Shell glob expansion exceeds the argument limitUse find /path -type f -delete instead of rm -rf /path/*
Reformatted filesystem still runs low on inodesbytes-per-inode ratio still too high for the workloadLower the ratio with mkfs.ext4 -i 2048 or switch to XFS

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need this available 24/7, keeping it healthy is a different job, since inode exhaustion tends to come back if nothing is watching for it. See how we run infrastructure like this for European teams.
#inodes #disk space #df #troubleshooting #linux filesystem #log rotation #monitoring

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. Fully managed, with one fixed contact who knows your setup.

You get one fixed contact who knows your setup

Rotterdam 11:46 · reachable in a message, no ticket form

Learn more: managed cloud infrastructure