Linux No space left on device (ENOSPC)
Encountering the "No space left on device" error means your disk partition has run out of free space or inodes; this guide explains how to diagnose and resolve it.
What This Error Means
The "No space left on device" error, indicated by the ENOSPC error code, is a critical system message in Linux that signals a storage-related problem. At its core, it means your operating system, or an application attempting to write data, has run out of available capacity on a particular filesystem. This isn't just about megabytes or gigabytes; it can mean one of two things: either there are no free data blocks left (the traditional understanding of "disk full"), or, less commonly but often more puzzlingly, there are no free inodes left. Inodes are data structures that store metadata about files and directories (permissions, ownership, timestamps, location of data blocks), and each file or directory requires one. When you hit ENOSPC, any operation that attempts to write new data or create new files will fail, often leading to application crashes, system instability, or even preventing crucial logs from being written.
Why It Happens
This error typically occurs because a filesystem has reached its maximum capacity. When an application tries to create a new file, write to an existing file, or even just create a temporary file, the operating system checks if there's enough space. If there isn't, the write operation fails with ENOSPC.
The "why" can often be traced back to either an overwhelming amount of data being written, or a lack of proper cleanup. For instance, log files might grow unchecked, temporary directories might accumulate junk, or a build process might create thousands of small intermediate files. In my experience, the less intuitive cause — inode exhaustion — often catches developers off guard. You might see df -h report plenty of free space, but df -i tells a different story: all inodes are used up. This happens when there are a huge number of very small files, each consuming an inode, even if they collectively don't use much block space.
Common Causes
In my career, I've seen ENOSPC crop up in various scenarios, and pinpointing the exact cause requires systematic investigation. Here are the most common culprits:
- Excessive Log Files: Uncontrolled logging is a frequent cause, especially in production environments. Application logs, web server access logs (Nginx, Apache), and system logs (
/var/log) can grow rapidly if not properly rotated and compressed. I've seen a single misconfigured application fill an entire root partition with gigabytes of verbose logs. - Accumulated Temporary Files: Many applications create temporary files for various operations. If these applications crash, are terminated improperly, or simply don't have robust cleanup routines,
/tmp,/var/tmp, and user-specific temporary directories (e.g.,~/.cache,~/.npm) can become bloated. - Application-Specific Data Overload: Databases with large datasets, caches that aren't pruned, or file upload directories that are never cleaned can consume vast amounts of space. For example, a development server might accumulate many old Docker images or Maven/Gradle caches.
- Failed Backups or Snapshots: Sometimes, backup processes fail midway, leaving partial backups that take up space. Or, older backups might not be automatically purged, leading to an accumulation of large archives.
- Package Manager Caches: Tools like
apt,yum, ordnfkeep downloaded package files in caches (/var/cache/apt/archives,/var/cache/yum) to speed up future installations. While useful, these can grow quite large over time. - Inode Exhaustion: This is trickier. It happens when you have millions of very small files. Common examples include mail queues, web server session files, build artifacts (e.g.,
node_moduleswith deep dependency trees, Java.classfiles), or file systems used for temporary storage by applications that generate many tiny files.gitrepositories with lots of small files, or even~/.configdirectories for some applications, can contribute.
Step-by-Step Fix
Addressing ENOSPC requires a systematic approach to identify and clear space. Always proceed with caution, especially when deleting files in production environments.
-
Identify the Full Partition:
Start by determining which filesystem is full. Thedf -hcommand will show disk usage in a human-readable format. Look for partitions with 100% (or close to 100%) usage.bash df -hYou might see something like:
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 20G 0 100% / tmpfs 3.9G 0 3.9G 0% /dev/shm
Here,/dev/vda1mounted at/(the root filesystem) is full. -
Check for Inode Exhaustion:
Ifdf -hshows plenty of free space but you're still gettingENOSPC, your problem is likely inode exhaustion. Usedf -ito check inode usage.bash df -iIf
IUse%is near 100% for the problematic partition, then you have too many small files. -
Find Large Files and Directories (for block space issues):
Navigate to the mount point of the full partition (e.g.,/or/var). Usedu -sh *to find the largest directories. Recursively drill down until you find the culprits. Remember to include hidden directories (.[!.]*).```bash
Start at the root (or problematic mount point)
sudo du -sh /
sudo du -sh /.[!.] # Check hidden directories at root, e.g., /.snapshotExample: if /var is large, check within /var
sudo du -sh /var/*
Example: if /var/log is large, check within /var/log
sudo du -sh /var/log/*
`` Once you've identified large files or directories, you can make informed decisions about deletion. For instance, I've often found massivedocker` directories or uncleaned database backups. -
Analyze Inode Usage (for inode issues):
Ifdf -iindicated inode exhaustion, you need to find where all the small files are. This command can help pinpoint directories containing a high number of files:bash sudo find /path/to/full/partition -xdev -printf '%h\n' | sort | uniq -c | sort -rh | head -n 20
Replace/path/to/full/partitionwith the actual mount point (e.g.,/). This command finds the 20 directories with the most files. -
Clear Temporary Files:
- System-wide temporary files:
sudo rm -rf /tmp/*(use with extreme caution, only if you're sure no critical applications are actively using files in/tmp). - User-specific caches:
rm -rf ~/.cache/*,~/.npm/_cacache,~/.gradle/caches. - Package manager caches:
- Debian/Ubuntu:
sudo apt clean - CentOS/RHEL:
sudo yum clean allorsudo dnf clean all
- Debian/Ubuntu:
- System-wide temporary files:
-
Rotate and Compress Logs:
- Manually delete old log files:
bash # Example: Delete .log files older than 7 days in /var/log/my_app sudo find /var/log/my_app -name "*.log" -mtime +7 -delete - Configure
logrotatefor critical applications. Most default installations handle system logs, but custom applications might need manual configuration. - Consider compressing older logs rather than outright deleting them:
gzip old_log_file.log.
- Manually delete old log files:
-
Identify and Remove Old/Unused Files:
- Find large files:
bash sudo find / -xdev -type f -size +1G -print0 | xargs -0 du -h | sort -rh | head -n 10
This lists the 10 largest files (over 1GB) on the root filesystem. Adjust-sizeand path as needed. - Docker cleanup:
docker system prune -a(removes all stopped containers, unused networks, dangling images, and build cache). This is often a massive space saver for Docker hosts. - Git repositories: Large
.gitdirectories, especially from monorepos or heavily history-rewritten repos, can be cleaned withgit gc --prune=now.
- Find large files:
-
Extend the Filesystem (Last Resort):
If cleanup isn't sufficient, or if the system is genuinely undersized, you might need to extend the filesystem. This usually involves:- Resizing the underlying logical volume (LVM) or cloud disk (e.g., AWS EBS, Azure Disks).
- Then, extending the filesystem itself (
resize2fsfor ext4,xfs_growfsfor XFS). This typically requires root privileges and some downtime, or at least unmounting/remounting for some file systems.
Code Examples
Check Disk Space Usage (Human Readable):
df -h
Check Inode Usage:
df -i
Find Top 10 Largest Directories from Current Location:
sudo du -sh * | sort -rh | head -n 10
Find Top 10 Directories with the Most Files (Inode-heavy check, from root):
sudo find / -xdev -printf '%h\n' | sort | uniq -c | sort -rh | head -n 10
Delete Log Files Older Than 30 Days in a Specific Directory:
sudo find /var/log/nginx -name "*.log" -mtime +30 -delete
Note: Be very careful with find ... -delete.
Clean APT Package Cache (Debian/Ubuntu):
sudo apt clean
Clean YUM/DNF Package Cache (CentOS/RHEL):
sudo yum clean all
# or
sudo dnf clean all
Remove Old Docker Images, Containers, Volumes, and Networks:
docker system prune -a
Find Largest Files (over 500MB) on the Root Filesystem:
sudo find / -xdev -type f -size +500M -print0 | xargs -0 du -h | sort -rh | head -n 10
Environment-Specific Notes
-
Cloud Environments (AWS EC2/EBS, Azure VMs/Disks, GCP Compute Engine/Persistent Disks):
Cloud providers make resizing volumes relatively straightforward. If you're consistently hittingENOSPC, it's often simpler and safer to increase the disk size than to aggressively prune. You'll typically increase the volume size through your cloud console, then connect to the instance and useresize2fsorxfs_growfsto extend the filesystem to use the newly available space. Monitor disk usage with cloud-native tools (e.g., CloudWatch, Azure Monitor) to proactively prevent issues. I often set up alarms forDiskUsage > 90%. -
Docker / Containerized Applications:
TheENOSPCerror can occur both inside a container and on the host filesystem where Docker stores its data.- Inside a container: If an application inside a container is logging excessively or creating many temporary files, it can fill up the container's writable layer. This typically means you need to adjust log rotation within the container or remap
/var/logto a dedicated host volume. - On the host: Docker itself can consume a lot of disk space for images, containers, and volumes. The
docker system prune -acommand is invaluable here for clearing out unused resources. Also, remember that volumes mounted from the host will still occupy space on the host's filesystem, so you'll need to check the host's disk usage directly.
- Inside a container: If an application inside a container is logging excessively or creating many temporary files, it can fill up the container's writable layer. This typically means you need to adjust log rotation within the container or remap
-
Local Development Machines:
Local dev environments are notorious forENOSPCdue to massivenode_modulesdirectories,target/folders from Java/Rust builds, VM images, or multiple Git repositories with extensive histories.npm cache clean --forceorpnpm store prunefor Node.js.cargo cleanfor Rust projects.- Deleting old VM images or snapshots.
- Manually review and delete large, no-longer-needed project directories.
Frequently Asked Questions
Q: df -h shows plenty of space, but I still get ENOSPC. What gives?
A: This is almost certainly an inode exhaustion issue. While you have free data blocks, you've run out of available inodes, meaning no new files (even tiny ones) can be created. Use df -i to confirm inode usage.
Q: Is it safe to delete files while the system is running?
A: Be cautious. Deleting a file that is still open and being used by a running process will mark the file for deletion, but its disk space won't be freed until the process completely releases its file handle. You might need to restart the process or even the service to free up space immediately. Use lsof | grep deleted to find open, deleted files.
Q: How can I prevent ENOSPC from happening again?
A: Implement proactive monitoring (e.g., df -h in cron jobs, Prometheus/Grafana alerts), configure logrotate for all critical applications, set up automated cleanup scripts for temporary directories or old backups, and periodically review application configurations that might lead to excessive file creation.
Q: My root partition is full, and I can't even install du or find to diagnose! What do I do?
A: This is a tricky situation. You'll likely need to boot into a rescue mode or a live Linux distribution (e.g., a USB stick). From there, you can mount your problematic root partition, and then use the tools available on the rescue system to diagnose and clean up. Minimal commands like rm and ls might still work if you can get a shell.