Linux Disk Full: How to Find and Free Disk Space on a VPS

15 min read 2 views 0
On this page

"No space left on device" breaks almost everything on a server: databases stop writing, websites return errors, package updates fail and sometimes you cannot even log in properly. This guide shows how to troubleshoot a Linux disk full problem step by step: confirm whether you ran out of blocks or inodes, find what uses the space with du and ncdu, then free disk space from the systemd journal, apt/dnf caches, Docker, oversized logs and deleted files that are still held open. Commands are given for Debian, Ubuntu, CentOS 7 and Rocky Linux / AlmaLinux 8 and 9.

Step 1: Check Disk Usage with df -hT and df -i

First find out which file system is full. A disk can also be "full" when it has free gigabytes but no free inodes, which happens when an application creates millions of tiny files.

df -hT          # space per file system, with type
df -i           # inodes: IUse% at 100% also means "No space left on device"

If Use% is at 100%, continue with Step 2. If only IUse% is high, jump to Step 9.

Step 2: Find Large Directories and Files with du

du totals the size of each directory. The -x option stays on one file system, so mounted disks and virtual file systems do not confuse the result. Work your way down level by level.

# Largest top-level directories on the root file system
du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -n 15
# Drill down into the biggest one, for example /var
du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 15

# Single files larger than 200 MB
find / -xdev -type f -size +200M -exec ls -lh {} + 2>/dev/null | sort -k5 -h

Step 3: Browse Disk Usage Interactively with ncdu

ncdu scans once and lets you navigate the tree with arrow keys, which is much faster than repeating du. If the disk is completely full and installation fails, free a little space with Step 4 or Step 5 first.

# Debian / Ubuntu
apt install -y ncdu
# Rocky Linux / AlmaLinux / CentOS (from EPEL)
dnf install -y epel-release && dnf install -y ncdu

ncdu -x /       # arrows to browse, d = delete, q = quit

Step 4: Shrink the systemd Journal (journalctl --vacuum)

On systems with a persistent journal, /var/log/journal can grow to several gigabytes. Vacuuming removes the oldest archived journal files safely, and a size limit stops the problem from coming back.

journalctl --disk-usage
journalctl --vacuum-size=200M      # or: journalctl --vacuum-time=14d

# Cap the journal permanently
mkdir -p /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/size.conf <<'EOF'
[Journal]
SystemMaxUse=200M
EOF
systemctl restart systemd-journald

Step 5: Clean the apt / dnf Cache and Old Kernels

Downloaded packages and old kernels are a quick win, especially on small cloud disks.

# Debian / Ubuntu
apt clean
apt autoremove --purge

# Rocky Linux / AlmaLinux 8, 9
dnf clean all
dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)   # keep 2 newest kernels

# CentOS 7
yum clean all
yum install -y yum-utils && package-cleanup --oldkernels --count=2

Step 6: Handle Large Log Files Safely

Application logs in /var/log or inside a website directory are a frequent cause of a full disk. Do not rm a log that a service is still writing to: the space will not be released (see Step 8). Empty it with truncate instead, then fix the rotation.

# Log files larger than 100 MB
find /var/log -type f -size +100M -exec ls -lh {} +

# Empty a live log WITHOUT deleting it (the service keeps writing)
truncate -s 0 /var/log/nginx/access.log

# Remove compressed rotated logs older than 14 days
find /var/log -type f -name '*.gz' -mtime +14 -delete

# Dry-run logrotate to see why a log is not rotating
logrotate -d /etc/logrotate.conf

Step 7: Reclaim Space from Docker (docker system prune)

Old images, stopped containers and build cache accumulate quickly in /var/lib/docker. Check first, then prune.

docker system df                   # what Docker is using
docker system prune                 # stopped containers, unused networks,
                                    # dangling images and build cache
docker system prune -a              # also ALL images not used by a container

# Container logs live here and can grow without limit
du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail
Never add --volumes unless you are sure: it permanently deletes unused Docker volumes, which often contain database data.

To keep container logs from filling the disk again, set a size limit:

# /etc/docker/daemon.json - limit future container logs
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "50m", "max-file": "3" }
}
# then: systemctl restart docker  (applies to newly created containers)

Step 8: Find Deleted but Still Open Files with lsof +L1

When df says the disk is full but du cannot find the space, a process is usually still holding a file that was deleted. The space only returns when that process closes the file.

# Debian / Ubuntu: apt install -y lsof   |   Rocky / AlmaLinux: dnf install -y lsof
lsof +L1
# COMMAND  PID  ...  SIZE/OFF  NLINK  NAME
# php-fpm  1234 ...  4.2G      0      /var/log/app/debug.log (deleted)

# Fix 1: restart the service holding the file
systemctl restart php-fpm
# Fix 2: truncate it through /proc (PID 1234, file descriptor 7 from "ls -l /proc/1234/fd")
: > /proc/1234/fd/7

Step 9: Fix "No Space Left" Caused by Full Inodes

When df -i shows 100%, look for directories with huge numbers of small files: PHP session folders, mail queues, cache directories or temporary uploads.

# Directories that contain the most files
find / -xdev -type f 2>/dev/null | cut -d/ -f2-4 | sort | uniq -c | sort -n | tail -n 10

Clean the directory responsible and configure the application to expire old files automatically.

FAQ

Why do df and du show different numbers?

Usually because of deleted files that are still open (Step 8). Files hidden underneath a mount point and the space ext4 reserves for root (normally 5%) can also explain the difference.

Is it safe to delete everything in /var/log?

No. Remove old rotated .gz files and truncate large active logs, but keep the directory structure; some services refuse to start if their log directory is missing.

My disk keeps filling up. Can I add storage?

Find the root cause first with the steps above. If the data really needs more room, JUSTG cloud servers can be upgraded with additional SSD space; check the upgrade options in the client area or submit a ticket.

If your Linux server is still out of disk space, please submit a ticket at https://www.justg.com/submitticket.php and JUSTG technical support will help you.

Was this answer helpful?