Back to tutorials
Tutorial

Disk Full on VPS: Find Large Files and Free Space Safely

Free disk space on Ubuntu safely: find what grew, clean logs, Docker, and databases, and keep your sites online.

By Anurag Singh
Updated on Nov 19, 2026
Category: Tutorial
Share article
Disk Full on VPS: Find Large Files and Free Space Safely

“No space left on device” usually shows up at the worst time. Deploys fail. WordPress won’t upload media. MySQL stops accepting writes, or Nginx refuses to reload.

This walkthrough shows how to free disk space on Ubuntu (and Debian) on a VPS in a controlled way. The goal is to regain headroom without deleting anything critical.

You’ll first confirm what’s actually full. Then you’ll work through the usual offenders: system journals, text logs, package caches, Docker storage, and oversized databases. Each step includes commands and quick checks, so you can confirm the cleanup worked.

Prerequisites and safety rules (read this first)

  • You have SSH access and sudo.
  • Assumes Ubuntu 22.04/24.04 LTS or Debian 12/13 on a VPS.
  • If the disk is truly at 100%, some services can’t write PID or temp files. Your first goal is to recover at least 1–2 GB.

Two rules: don’t delete randomly from /var/lib. Don’t remove files under /var/www unless you know exactly what they are.

If you’d rather not be the on-call storage janitor, a managed VPS hosting plan can cost less than the downtime from a bad cleanup.

Step 1: Confirm what’s full (filesystem vs inode)

Run these checks first. They tell you whether you’re out of disk bytes or out of inodes.

df -hT

Find the filesystem mounted on /. On some setups, the hot spot is /var.

df -ih

If IUse% is 100%, you ran out of inodes. That means “too many small files,” not “too many bytes.”

Most cleanup below still applies. For inode pressure, focus on caches, spools, and temp directories.

Verify: after freeing a little room, confirm the system can write again.

sudo sh -c 'echo ok > /root/disk-test.txt' && sudo rm -f /root/disk-test.txt

Step 2: Find the top-level directory that grew

This is the fastest “where did it go?” view on most VPS setups.

sudo du -xhd1 / | sort -h

-x keeps the scan on the same filesystem. It also prevents du from wandering into mounted backups or network volumes.

Big directories you’ll see often:

  • /var (logs, Docker, databases)
  • /home (uploads, backups, build artifacts)
  • /usr (packages, snaps)

Next, drill into the largest directory. For example, if /var is the problem:

sudo du -xhd1 /var | sort -h

Mistake to avoid: running du across /proc or /sys without -x. The output is noisy and doesn’t reflect real disk usage.

Step 3: Locate individual large files fast (logs, core dumps, database files)

When you’re close to full, start by listing the worst offenders.

sudo find / -xdev -type f -size +500M -printf '%s\t%p\n' 2>/dev/null | sort -nr | head -n 30

Verify: these paths show up often: /var/log, /var/lib/docker, /var/lib/mysql, /var/lib/postgresql, and /var/lib/redis.

If the biggest files live there, you’ve already narrowed the cause.

Step 4: If you deleted files but space didn’t return (open file handles)

This is a common trap. You delete a huge log file, but a running process still has it open. The filesystem won’t free that space until the process releases the handle.

sudo lsof +L1 | head -n 50

If you see a large (deleted) entry, restart the owning service. Example for Nginx:

sudo systemctl restart nginx

Verify:

df -h

If this keeps happening, log rotation is usually broken. Another common cause is an app logging to an unexpected path.

If you run Nginx with WordPress, also see: Set Up Fail2ban for Nginx on Ubuntu VPS (heavy bot traffic can explode access logs).

Step 5: Clean systemd journal logs safely

On many VPS images, the systemd journal grows quietly. You often notice it only when the disk fills.

Check current usage:

sudo journalctl --disk-usage

Keep only 7 days:

sudo journalctl --vacuum-time=7d

Or cap it to 500 MB:

sudo journalctl --vacuum-size=500M

Verify:

sudo journalctl --disk-usage
df -h /

Common mistake: deleting files in /var/log/journal by hand. Use journalctl so the journal stays consistent.

Step 6: Fix huge text logs (Nginx/Apache/PHP-FPM) without breaking rotation

Start by finding which logs are actually large.

sudo du -h /var/log/* 2>/dev/null | sort -h | tail -n 30
sudo du -h /var/log/nginx/* 2>/dev/null | sort -h | tail -n 30

If one file is enormous, truncate it instead of deleting it. Truncation keeps the file handle valid, so the service keeps writing without confusion.

sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log

Reload the service so logging continues normally:

sudo nginx -t && sudo systemctl reload nginx

Verify: make a request to your site. Then confirm the logs are small and updating.

sudo ls -lh /var/log/nginx/*.log
sudo tail -n 5 /var/log/nginx/access.log

If logs grow aggressively, you likely need rate limiting or better bot protection. For WordPress login abuse, see: Set Up Fail2ban For WordPress Login On Nginx.

Step 7: Clear apt cache and old packages (quick wins on Ubuntu/Debian)

On long-lived servers, package caches and old dependencies can add up.

sudo apt-get clean
sudo apt-get autoremove --purge -y

If you’re on Ubuntu and the image includes Snap, check whether Snap storage is a factor:

snap list --all 2>/dev/null | wc -l
df -h /var/lib/snapd 2>/dev/null || true

Verify:

df -h /

Step 8: Docker cleanup (images, containers, volumes) without deleting production data

Docker storage is a frequent culprit. This is common with Compose stacks and frequent deploys.

Start with a snapshot of what’s being used:

docker system df

You’ll get a breakdown of images, containers, volumes, and build cache.

Safe cleanup #1: remove dangling images and build cache

docker image prune -f
docker builder prune -f

Safe cleanup #2: remove stopped containers

docker container prune -f

Be careful with volumes: volumes often hold databases and uploads. Don’t run docker volume prune unless you’re certain those volumes are unused.

List volumes and review their on-disk usage:

docker volume ls
sudo du -sh /var/lib/docker/volumes/* 2>/dev/null | sort -h | tail -n 20

If you spot an abandoned stack, remove it from its project directory. That way, networks and volumes get handled consistently:

cd /opt/your-compose-project
docker compose down

Verify:

docker system df
df -h /

If Docker keeps ballooning because deploys leave behind cache and old images, a Git-based deployment platform can enforce retention more predictably. See: Install Coolify on a VPS for Git-Based App Deploys.

Step 9: Database storage triage (MySQL/MariaDB and PostgreSQL)

Databases are not the place for filesystem “cleanup.” Don’t delete files from database directories.

The goal is simple. Find what’s large, then reclaim space with SQL and planned maintenance.

MySQL/MariaDB: find large databases and tables

First, confirm the datadir (often /var/lib/mysql):

sudo mysql -e "SHOW VARIABLES LIKE 'datadir';"

List database sizes:

sudo mysql -e "SELECT table_schema AS db, ROUND(SUM(data_length+index_length)/1024/1024,1) AS mb FROM information_schema.tables GROUP BY table_schema ORDER BY mb DESC;"

For one database, list the largest tables:

DB=yourdb
sudo mysql -e "SELECT table_name, ROUND((data_length+index_length)/1024/1024,1) AS mb FROM information_schema.tables WHERE table_schema='${DB}' ORDER BY mb DESC LIMIT 20;"

Common culprits on web servers: WordPress wp_options autoload bloat, WooCommerce action scheduler tables, analytics tables, and job/queue tables from apps.

Reclaiming space: deleting rows doesn’t always shrink files right away. With modern MariaDB/MySQL defaults and file-per-table, OPTIMIZE TABLE can reclaim space on bloated tables.

OPTIMIZE TABLE also takes a lock, so schedule it for low traffic.

sudo mysql -e "OPTIMIZE TABLE yourdb.yourtable;"

If you need remote app access, keep it locked down and firewalled. This tutorial walks through a safer setup: Set Up MariaDB on Ubuntu for Remote App Access.

PostgreSQL: find what’s largest

List database sizes:

sudo -u postgres psql -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;"

Inside a database, list the largest relations (tables/indexes):

DB=yourdb
sudo -u postgres psql -d "$DB" -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) AS size FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 20;"

Reclaiming space: VACUUM (ANALYZE) helps with churn, but it won’t always shrink files on disk. VACUUM FULL can shrink files, but it takes an exclusive lock.

Don’t run VACUUM FULL on production without planning.

sudo -u postgres psql -d "$DB" -c "VACUUM (ANALYZE);"

Step 10: Find and clean “forgotten backups” and build artifacts

Old tarballs, duplicated dumps, and CI leftovers often hide under /home or /opt.

sudo du -xhd1 /home | sort -h
sudo find /home -type f -name '*.sql' -o -name '*.sql.gz' -o -name '*.tar.gz' 2>/dev/null | head

Before you delete anything, confirm size and timestamp:

sudo ls -lh --time-style=long-iso /home/*/*.sql* 2>/dev/null | head -n 30

Safer pattern: move suspicious large files into quarantine first. Delete them after a day of normal operations.

sudo mkdir -p /root/quarantine
sudo mv /home/youruser/old-backup-2026-01-01.sql.gz /root/quarantine/

Verify: confirm your app still starts and behaves. Then remove the quarantined file later:

sudo rm -f /root/quarantine/old-backup-2026-01-01.sql.gz

Step 11: Handle WordPress uploads and cache directories carefully

WordPress disks usually fill for three reasons: media uploads, plugin-made backups, or cache directories that never get cleaned.

Check the typical paths:

sudo du -sh /var/www 2>/dev/null
sudo du -sh /var/www/* 2>/dev/null | sort -h | tail -n 20
sudo find /var/www -type d \( -name cache -o -name backups -o -name upgrade \) -maxdepth 5 2>/dev/null | head -n 50

If a backup plugin writes daily archives into wp-content, you may be storing “backups of backups” on the same disk. Once you have a real offsite plan, disable local backup archives or lower retention.

Also check permissions before blaming disk issues for WordPress write errors. This guide walks you through safe fixes: WordPress File Permissions Fix on a VPS.

Step 12: Prevent the next outage (logrotate, Docker limits, and tested backups)

After you recover headroom, take a few minutes to stop the next disk-full incident. You want a stable baseline, not recurring emergencies.

Set sane log retention

Confirm logrotate is installed and running:

systemctl status logrotate.timer 2>/dev/null || true
sudo test -f /etc/logrotate.conf && echo "logrotate present"

Preview what logrotate would do:

sudo logrotate -d /etc/logrotate.conf | head -n 60

If your Nginx logs aren’t rotating, check for /etc/logrotate.d/nginx. Make sure it uses postrotate to reload Nginx.

Cap Docker log growth (common on chatty apps)

Docker’s default JSON logs can grow without limit. Put a cap in /etc/docker/daemon.json:

sudo mkdir -p /etc/docker
sudo nano /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "3"
  }
}

Restart Docker. This restarts containers on many setups, so plan a brief maintenance window.

sudo systemctl restart docker

Verify:

docker info | grep -i "Logging Driver"

Backups that include restore tests

Disk emergencies often expose weak backup routines. If you’ve never restored a backup, you don’t know if it works.

For MySQL/MariaDB, follow a process that includes tested restores: Set Up Automatic MySQL Backups With Tested Restores.

Quick checklist: the safe “free space” order of operations

  1. Confirm full bytes vs inodes: df -hT, df -ih
  2. Find largest directory: du -xhd1 /
  3. Identify huge files: find / -xdev -type f -size +500M
  4. Release deleted-but-open files: lsof +L1
  5. Vacuum journal: journalctl --vacuum-time=7d
  6. Truncate monster logs, reload services
  7. Clean apt cache: apt-get clean, autoremove
  8. Docker prune images/build cache (avoid volume prune unless verified)
  9. Database triage via SQL, not filesystem deletions
  10. Fix the cause: rotation, caps, retention, backups

Where this fits on real hosting (VPS vs managed)

On a self-managed VPS, you’re in control. You clean up now, then you put guardrails in place.

If you run multiple sites, Docker stacks, and a database on one box, you’ll hit capacity sooner than you expect. That’s usually your cue to expand the disk or split roles across services.

If you’d rather hand off emergency triage and routine maintenance (updates, monitoring, log retention), consider HostMyCode managed VPS hosting. If you just want clean Linux resources for your own stack, a HostMyCode VPS gives you root access and predictable performance.

If your VPS keeps drifting back to 95–100% disk usage, treat it as a planning problem, not just a cleanup task. HostMyCode can provision a right-sized VPS for your stack, or you can hand off the ongoing maintenance to managed VPS hosting so disk usage, logs, and backups stay predictable.

FAQ

How much free space should I keep on an Ubuntu VPS?

Keep at least 10–15% free, or a minimum of 5–10 GB on small disks. Databases and package updates need temporary working room.

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

Prefer truncation (truncate -s 0) and proper rotation. Deleting can leave space “stuck” if a process still has the file open; check with lsof +L1.

Can I run docker system prune -a?

It’s safe only if you understand what it removes. It can delete images you expect to keep for rollback. Start with docker system df, then prune in smaller steps.

Why didn’t my disk usage drop after I deleted a huge file?

A service may still hold the deleted file open. Use sudo lsof +L1 and restart that service to release the space.

What should I do if /var/lib/mysql is the biggest directory?

Don’t delete files from the datadir. Identify the largest databases/tables via SQL, then clean at the application level or schedule maintenance (like OPTIMIZE TABLE) during low traffic.

Summary

To free disk space on Ubuntu safely, you need visibility and discipline. First, find where the bytes went. Then clean up in ways that won’t break running services or corrupt data.

Use df and du to narrow the cause. Take the quick wins (journal, logs, caches). Then handle Docker and databases with their own tools.

If you want a VPS setup that’s easier to scale when storage gets tight, start with a HostMyCode VPS. And if you need help moving a crowded server to a cleaner layout, HostMyCode migrations can help.

Disk Full on VPS: Find Large Files and Free Space Safely | HostMyCode