The message No space left on device appears when MySQL stops writing, Nginx cannot save its logs, or apt fails in the middle of an installation. It does not always mean that the disk is full of data: there are three different causes, and each is resolved differently.
First, figure out which of the three causes applies to you
Compare the use of blocks (space) with that of inodes (number of files):
df -hTdf -i- 100% utilization in
df -hT: no free blocks remain. Continue in Section A. - There is free space, but
IUse%is 100% full indf -i: the inodes have been exhausted. Go to section B. dfsays the disk is full, butdudoesn't find that much data: there are deleted files that some process is keeping open. Section C.
A. The disk is full of data
Find the largest folders without leaving the root partition (-x excludes other mounted file systems):
sudo du -xh --max-depth=2 / 2>/dev/null | sort -h | tail -n 20On a VPS, these are the usual suspects—and here's the safe way to eliminate them.
systemd log entries
journalctl --disk-usagesudo journalctl --vacuum-size=200MTo prevent it from growing indefinitely again, add the line SystemMaxUse=200M to the file /etc/systemd/journald.conf and restart the service using sudo systemctl restart systemd-journald.
Cache of apt and kernels you no longer use
sudo apt cleansudo apt autoremove --purgeDocker Images and Layers
docker system dfdocker image prune -adocker builder prunedocker image prune -a Deletes all images that are not used by any container, including those from stopped containers that you plan to restart. Check docker ps -a first.
Application logs
If an active log file is several GB in size, flush it instead of deleting it; that way, the process continues to write to the same file:
sudo truncate -s 0 /var/log/nginx/access.logB. We've run out of inodes
Every file uses up one inode, no matter how small it is. Millions of tiny files (PHP sessions, an application's cache, a backed-up mail queue) can use them all up even when the disk is half empty. Look for the folders with the most files:
sudo du --inodes -x -d 3 / 2>/dev/null | sort -n | tail -n 15When you find the folder, delete files by age using find; a rm * on hundreds of thousands of files results in an "Argument list too long" error:
sudo find /var/lib/php/sessions -type f -mtime +2 -deleteThen fix the source: a session collector that isn't running or a cache with no expiration time will fill it up again.
C. Deleted files that are still taking up space
If you deleted a huge log file using rm while the service had it open, the space isn't freed up until the process closes the file. List them:
sudo lsof -nP +L1Column COMMAND indicates which service is holding the space. When you restart it (for example, sudo systemctl restart nginx), the space is immediately released.
What You Should Never Delete Manually
- MySQL or MariaDB binlogs: Delete them from the database server itself using
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 3 DAY;to ensure that the index remains consistent. - The contents of
/var/lib/docker: Always use theprunecommands; deleting folders in overlay2 corrupts the images. /bootFiles: Remove old kernels usingapt autoremove, neverrm.
ext step
If, after cleaning it up, the disk fills up again within a few weeks, the problem is one of capacity, not maintenance: choose a Cloud VPS with more storage.