Skip to main content

Linux Server Disk Full: What to Delete and What to Keep

Disk full on a Linux server? Find what is eating space (journal, logs, Docker, apt, snaps, deleted-but-open files) and clean it safely with real commands.

When a Linux server disk is full, delete the things that can be recreated, not data. That means archived systemd journal files, old rotated logs, unused Docker images and build cache, the apt package cache, and old snap revisions. First measure with df and du so you know which one is actually the problem. Also check for deleted files that a process still holds open. We do this often from DockLab, and the order below is the one that has stopped us from deleting the wrong thing.

Quick answer

  • Find the full filesystem and check inodes: df -hT and df -i, then sudo du -xh --max-depth=1 / | sort -h.
  • Shrink the journal safely: sudo journalctl --rotate && sudo journalctl --vacuum-size=500M.
  • Reclaim Docker space: docker system df, then docker image prune -a and docker builder prune. Leave volumes alone unless you know what's in them.
  • Clear package leftovers: sudo apt-get clean and sudo apt autoremove --purge.
  • Free space held by deleted files: sudo lsof -nP +L1, then restart the process that holds them.

Step 1: measure before you delete anything

A full disk is rarely the disk you think. /var might be its own mount, /boot might be a tiny partition full of old kernels, or you might be out of inodes while df -h still shows free space. Start here:

df -hT
df -i

df -hT shows each mount with its filesystem type. df -i shows inode usage. If IUse% is at 100% but space is fine, the cause is millions of small files (sessions, mail queue, cache shards), not big files. Count inodes per directory instead of bytes:

sudo du --inodes -x --max-depth=1 / 2>/dev/null | sort -n | tail -15

For space, walk down from the full mount. The -x flag keeps du on one filesystem, so it won't wander into /proc or a mounted backup volume:

sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -15
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -15

If you can install packages, ncdu is faster to browse interactively: sudo apt install ncdu, then sudo ncdu -x /. To list single large files directly:

sudo find / -xdev -type f -size +500M -exec ls -lh {} + 2>/dev/null

A note on the last few percent. ext4 reserves blocks for root, and per the tune2fs man page the default is 5%, so that privileged daemons can keep writing when the filesystem is almost full. As a result, an app running as www-data or postgres can fail with "No space left on device" while root can still write. Don't lower the reserve on the root filesystem just to make the error go away.

Step 2: check for deleted files that still hold space

This catches a lot of people. Someone runs rm on a 20 GB log, du stops counting it, and df still says 100%. The file is gone from the directory, but the process writing to it still has it open, so the kernel can't free the blocks.

sudo lsof -nP +L1

+L1 lists open files with a link count below 1, which means deleted but still open. The SIZE/OFF column shows how much each one holds. You have two fixes:

  • Restart the service that owns the PID (sudo systemctl restart <service>). This is the clean option.
  • If you can't restart it right now, truncate the file through the open descriptor: sudo truncate -s 0 /proc/<PID>/fd/<FD>, using the FD number from the lsof output without its letter suffix.

Step 3: the safe-to-delete list, in order

These are the usual causes on Ubuntu and Debian servers, roughly in the order we check them.

The systemd journal

journalctl --disk-usage
sudo journalctl --rotate
sudo journalctl --vacuum-size=500M

The order matters. The journalctl man page says vacuuming only removes archived journal files, never active ones. Without --rotate first, a vacuum can end up freeing much less than you expected. You can use --vacuum-time=14d instead of a size limit if you'd rather keep logs by age.

Rotated and runaway logs in /var/log

Files like syslog.2.gz or nginx/access.log.5.gz are logrotate's old archives. You can delete them once you're sure no one needs them for an investigation:

sudo find /var/log -type f \( -name "*.gz" -o -name "*.[0-9]" \) -mtime +14 -print
# review the list, then run the same command with -delete instead of -print

If a single live log is huge, truncate it (see the callout above), then find out why it grew. A debug log level left on in production, or an error repeating in a loop, will fill the disk again within hours. If the log is full of upstream errors, our Nginx 502 Bad Gateway diagnostic order is the next stop. A full disk and 502s often show up together, because a crashed upstream can't write anything.

Docker images, build cache and container logs

docker system df
docker builder prune
docker image prune -a

docker system df breaks usage down by images, containers and local volumes, with a reclaimable column (docs). Build cache on a server that builds images on every deploy is often the biggest single item.

Be careful with docker system prune. By default it removes stopped containers, unused networks, dangling images and unused build cache. With -a it removes all unused images, and with --volumes it also removes unused anonymous volumes (docs). "Stopped" includes a container you stopped on purpose and planned to start again. You can limit it by age, for example docker system prune --filter "until=168h".

Container logs are the other big source of Docker disk usage. With the default json-file driver, max-size defaults to unlimited (docs), so a noisy container keeps writing to /var/lib/docker/containers/<id>/<id>-json.log until the disk is full:

sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log' | sort -h | tail -5
sudo truncate -s 0 /var/lib/docker/containers/<id>/<id>-json.log

apt cache and old kernels

du -sh /var/cache/apt/archives
sudo apt-get clean
sudo apt autoremove --purge

apt-get clean empties downloaded .deb files, which apt can download again. apt autoremove --purge removes packages that are no longer needed, including old kernels. That's usually the fix when /boot is full. Don't delete files in /boot by hand: you can end up with a broken boot entry and a server that won't come back after a reboot.

Old snap revisions

On Ubuntu, snapd keeps disabled older revisions of each snap. List them and remove each one:

snap list --all | awk '/disabled/{print $1, $3}'
sudo snap remove <name> --revision=<rev>
sudo snap set system refresh.retain=2

Core dumps and temp files

Crashing services can leave large dumps in /var/lib/systemd/coredump (coredumpctl list shows them). Check /tmp and /var/tmp for stale build artifacts too. Leave out sockets and lock files that belong to running services.

What not to delete

LocationDelete?Safe alternative
Archived journal filesYesjournalctl --rotate then --vacuum-size
/var/log/*.gz, *.1Yes, after reviewfind ... -mtime +14 -delete
Live log fileNo rmtruncate -s 0
/var/cache/apt/archivesYesapt-get clean
Unused Docker images, build cacheYesdocker image prune -a, docker builder prune
Docker volumesOnly if you know what is insidedocker volume ls, inspect first
/var/lib/docker/overlay2 by handNeverUse Docker's prune commands
/var/lib/mysql, /var/lib/postgresqlNever by handPURGE BINARY LOGS for MySQL binlogs, fix WAL retention for Postgres
Files in /bootNever by handapt autoremove --purge
Backups in home directoriesMove themCopy off-server, verify, then delete

The database rows are the expensive mistakes. On a busy database server, binlogs or WAL often look like "just logs", and deleting them by hand can corrupt replication or recovery. Use the database's own commands.

Step 4: make it not happen again

Cleanup only buys time. Set caps on the things that grow without limit.

  1. Cap the journal

    Create /etc/systemd/journald.conf.d/size.conf. Drop-ins in /etc override the defaults (journald.conf). The default SystemMaxUse is 10% of the filesystem, capped at 4G, which is a lot on a 25 GB VPS.

    [Journal]
    SystemMaxUse=500M
    

    Then run sudo systemctl restart systemd-journald.

  2. Rotate Docker container logs

    Put this in /etc/docker/daemon.json and restart Docker. The values have to be quoted strings.

    {
      "log-driver": "json-file",
      "log-opts": {
        "max-size": "10m",
        "max-file": "3"
      }
    }
    

    Existing containers keep their old log settings. You have to recreate them (docker compose up -d --force-recreate) to pick up the new ones.

  3. Prune build cache on a schedule

    On servers that build images during deploys, add docker builder prune -f --filter "until=168h" to a weekly cron job or systemd timer.

  4. Alert before 100%

    Any monitoring that warns at 80% gives you a day to react instead of a 3 a.m. outage. If legitimate data keeps growing, resize the volume rather than deleting more every week.

If you do this across several servers, keep each host's checks in one place. In DockLab, the Resource Monitor plugin shows live disk stats for the connected host next to the terminal. The Docker Assistant lists images, containers and volumes, so you can see what a prune would touch before you run it. Saved workspaces keep one tab per server, which helps when you're working through the same checklist on ten machines (our guide on managing multiple servers at once covers that setup). You can do everything above with plain ssh too. DockLab just puts the terminal, SFTP browser and these panels in one window. You can download DockLab to try it on your own hosts.

If you paste du or lsof output into an AI assistant to help you decide what to remove, read our notes on what to share with AI when debugging servers first, and check every delete command yourself before you run it.

Frequently Asked Questions

Why does df still show the disk full after I deleted files?

A process still has the deleted file open, so the kernel can't free its blocks. Run sudo lsof -nP +L1 to find it, then restart that service or truncate the file through /proc/<PID>/fd/<FD>. This happens most often with a log file that was removed with rm.

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

Rotated archives (*.gz, *.1) are safe to delete once nobody needs them for an investigation. Truncate live log files instead of deleting them, and keep the directory structure, because some services won't start if their log directory is missing. For the systemd journal, use journalctl --vacuum-size instead of deleting files by hand.

Is docker system prune safe on a production server?

The default command keeps volumes, so named data like database volumes stays. It does remove stopped containers, which can include a service you stopped on purpose, and with -a it removes every image not used by a container. Run docker system df first and add --filter "until=168h" if you want a safer margin.

What should I do when /boot is full?

Run sudo apt autoremove --purge, which removes kernels the package manager no longer needs. If apt itself fails because /boot is full, check dpkg -l 'linux-image*' and purge a specific old kernel with sudo apt purge linux-image-<version>, but never the running one (uname -r). Don't delete files in /boot by hand.

Why is my disk full when df -h shows free space?

You're probably out of inodes, not bytes. Check with df -i, then find the directory with the most files using sudo du --inodes -x --max-depth=1 /. The usual causes are PHP session directories, a stuck mail queue, or a cache that writes one tiny file per entry.

Try it on your own servers

SSH, SFTP, a remote editor, a browser and AI in one desktop workspace.

DockLab runs on macOS, Windows and Linux. Start free and keep your whole server workflow in one window.

About the author

· Co-founder, Legacies

Alexandru works on DockLab product and operations, with a focus on how technical teams run their servers day to day.