No Space Left on Device: Finding What Filled the Disk With du and ncdu

df tells you it's full; du and ncdu tell you why

Contents

No space left on device is one of the more honest error messages Linux gives you, and also one of the least useful on its own, because it tells you the disk is full without the faintest hint of what filled it. The service that just failed to write its next log line is rarely the culprit; more often it’s the innocent bystander that happened to be the one still trying to write when the last free byte disappeared. Finding the actual cause is a short, repeatable process, and there are two edge cases — inode exhaustion and space held by deleted-but-still-open files — where df genuinely lies to you about there being room, in a way that trips up people who’ve done this a hundred times.

Start wide, then narrow

df -h is the first command, and it answers a narrower question than people assume: which filesystem is full, not which directory. On a host with separate partitions for /, /var, /home and maybe a second data disk, this matters immediately — the fix is entirely different depending on whether it’s the root filesystem or a secondary mount that’s out of room:

The inside of a hard disk drive with its cover removed, showing the platter stack and actuator arm.
What the filesystem sits on: a spinning platter and an actuator arm, with the cover removed. Photo: Inside of hard disk drive by Radoslaw Ziomber, CC BY-SA 3.0, via Wikimedia Commons.
1
2
3
4
5
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        50G   48G  0G  100% /
/dev/sdb1       2.0T  1.8T  180G  91% /mnt/data
tmpfs           7.8G     0  7.8G   0% /dev/shm

Here the root filesystem is the one that’s actually full, not the larger data disk sitting at a comfortable 91%. That’s a common surprise on a homelab box where the OS disk was undersized on the assumption “the OS itself doesn’t need much space” — true right up until Docker’s image layers, container logs, and journald’s own log storage all land on that same root filesystem by default, quietly eating the margin that was meant to last years.

Once you know which filesystem, du narrows down where on it:

1
2
3
4
5
$ du -sh /* 2>/dev/null | sort -rh | head -10
23G     /var
14G     /usr
6.2G    /home
1.1G    /opt

Then repeat one level deeper into whichever directory is largest — du -sh /var/* 2>/dev/null | sort -rh | head -10 — and keep descending until a single directory or file is clearly disproportionate. This works, and for a quick one-off check it’s often all you need, but it’s slow on a large tree (du has to walk every file to sum sizes) and tedious to repeat manually at each level.

ncdu: the same idea, interactively

ncdu (NCurses Disk Usage) does the same walk du does but presents it as a navigable tree you can drill into with arrow keys, sort by size instantly, and delete files directly from within — genuinely faster than repeated du invocations once a tree is more than two or three levels deep, which in practice is almost always:

1
2
$ sudo apt install ncdu
$ sudo ncdu /

Point it at the filesystem root (or the specific mount that df flagged) and let it finish its initial scan — on a large disk with millions of small files this can take a while, and there’s no way around that; it genuinely has to stat every file, the same cost du pays, just amortised into one interactive session instead of many repeated command invocations. Once it’s done, sort by size (s toggles), drill into the largest entry with Enter, and repeat. ncdu also has an -x flag to stay on one filesystem, useful when scanning / on a box with several mounts under it that you don’t want counted twice or that would take much longer to include.

The directories that turn up disproportionately large on a self-hosting box, roughly in order of how often I’ve hit each one personally: Docker’s overlay2 storage under /var/lib/docker, container and journald logs, apt’s package cache, and old kernel versions nobody’s cleaned up after an upgrade.

The usual suspects, and what to do about each

Docker’s overlay2 layers and dangling images. docker system df gives a Docker-aware summary that’s more useful than raw du here, because it separates images, containers, local volumes and build cache — categories du alone can’t distinguish, since they’re all just files under the same /var/lib/docker tree to it:

1
2
3
4
5
6
$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          34        12        18.2GB    11.4GB (62%)
Containers      12        8         420MB     180MB (42%)
Local Volumes   9         6         6.1GB     900MB (14%)
Build Cache     102       0         3.8GB     3.8GB (100%)

A RECLAIMABLE figure that large is a strong signal before you even run a cleanup. For the actual reclaim mechanics — prune scope, what’s safe to remove without touching live data — see Reclaiming disk from Docker: images, volumes, build cache and dangling junk, which covers this specific category in depth rather than repeating it here.

Container and journald logs. Docker’s default json-file log driver has no size cap unless you’ve explicitly configured one, and a chatty container logging continuously for months can produce a single log file in the gigabytes. docker inspect <container> --format '{{.LogPath}}' finds it directly; Docker logs are eating my disk: log rotation done properly covers setting max-size/max-file so this stops recurring rather than treating it as a one-off cleanup. Journald has the same failure mode independently of Docker, capped by its own retention settings rather than anything Docker controls — see journald is eating the disk: log retention and vacuuming explained for that half of the picture.

apt’s package cache. Every .deb downloaded during an install or upgrade sits in /var/cache/apt/archives indefinitely unless cleaned. du -sh /var/cache/apt/archives shows the damage; apt clean clears it, and it’s harmless to run any time — nothing depends on those cached packages being present.

Old kernels. Each kernel upgrade on Debian/Ubuntu-family systems leaves the previous one installed by default (a deliberate safety net, so a bad kernel isn’t unrecoverable), and after several years of upgrades without cleanup this adds up to a genuinely large amount of /boot and /usr/lib/modules space. dpkg -l 'linux-image-*' lists what’s installed; remove old ones with apt autoremove --purge, keeping at least the current and one fallback kernel rather than pruning down to just the one currently running.

When df says there’s space but writes still fail

Two situations produce exactly the symptom this post opened with — No space left on device — while df -h shows free space, and both are worth knowing before you assume the error message is simply wrong.

Inode exhaustion. Every file and directory consumes an inode regardless of its size, and a filesystem has a fixed inode count set at creation time, independent of how much raw space is free. A directory tree with millions of tiny files — a badly configured cache, a mail spool, certain application session-storage patterns — can exhaust available inodes while gigabytes of raw space remain untouched. Check with df -i rather than df -h:

1
2
3
$ df -i /var
Filesystem      Inodes  IUsed    IFree IUse% Mounted on
/dev/sda2      3276800 3276800       0  100%  /var

IUse% 100 with plenty of raw space free elsewhere is the unambiguous signature. The fix is finding and pruning whatever created millions of small files — find /path -type f | wc -l per suspect directory narrows it down fast — since resizing the inode table itself generally means reformatting.

Deleted-but-still-open files. Deleting a file on Linux removes its directory entry, but a process that still has the file open keeps its actual disk blocks allocated until that process closes it or exits — the space genuinely isn’t freed yet, even though ls and every path-based tool report the file gone. This is a common cause of “I deleted a huge log file and df didn’t change” confusion, usually because the process that was writing to it (still) has the file descriptor open and hasn’t been told to reopen. lsof +L1 lists exactly these — open file descriptors pointing at files with a link count of zero, i.e. already deleted but still consuming space:

1
2
3
$ sudo lsof +L1
COMMAND   PID   USER   FD   TYPE DEVICE  SIZE/OFF NLINK NODE NAME
nginx    1842 wwwrun   17w   REG   8,2  4200000000     0 5512 /var/log/nginx/access.log (deleted)

That single deleted-but-open file is holding 4.2GB. Restarting the process that has it open (or sending it whatever signal makes it reopen its log file, SIGHUP for a lot of daemons including nginx) releases the space immediately, without needing to find and delete anything, because there’s nothing left to find — the directory entry is already gone.

Why du and ls -l sometimes disagree

A less common but genuinely confusing wrinkle: ls -l reports a file’s logical size, while du reports the actual disk blocks it consumes, and for sparse files — disk images, some database files, anything created with holes rather than written byte-for-byte — those two numbers can differ by an order of magnitude. A 100GB virtual disk image for a VM might show 100G under ls -l and du -h might report 4.2G, because most of it was never actually written and the filesystem never allocated real blocks for the unwritten regions. This is a normal consequence of how sparse files work on ext4, XFS and most other Linux filesystems, and it means eyeballing ls -l output while hunting for what filled a disk can send you chasing the wrong file entirely — always trust du’s number for what’s actually consuming space. The reverse confusion happens too: du --apparent-size deliberately reports the logical size instead of blocks-on-disk, useful specifically when you want to compare against ls -l rather than against real consumption, but easy to reach for by accident and get numbers that don’t match your expectations.

Preventing the next full disk rather than just fixing this one

Finding what filled the disk this time doesn’t stop it filling again with the same thing in six months, and the recurring offenders above — Docker layers, logs, package caches — are exactly the kind that creep back gradually enough that nobody notices until the same 3am alert fires again. Two cheap habits close most of that gap. First, a scheduled docker system prune (with an appropriate filter — unrestricted pruning can remove images you still want, so scope it to dangling images and build cache older than a set age rather than everything) keeps the largest recurring offender from accumulating unattended. Second, actual disk-usage alerting rather than discovering the problem via a failed write: a lightweight monitoring stack watching free space and firing before a filesystem hits 100% turns this into a calm scheduled cleanup instead of an outage. Netdata in an afternoon: per-second metrics, zero config covers a low-effort way to get that visibility without standing up a full Prometheus and Grafana stack just to watch one number.

For the specific case of a disk that fills gradually over weeks rather than suddenly, a periodic ncdu scan saved to a file and diffed against the previous run is a cheap way to spot a growth trend before it becomes an emergency — a directory quietly growing by a gigabyte a week is far easier to deal with as “I noticed this trending up” than as “the disk is full and everything just stopped writing”.

A worked example: chasing a full /var without ncdu installed

ncdu isn’t always available — a minimal container base image or a locked-down production host might not have it, and pulling in a new package while the disk is already full is its own small headache (apt install needs scratch space too). Here’s the same investigation done with nothing but du, on a /var that’s filled unexpectedly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ df -h /var
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G   19G  0G  100% /var

$ du -sh /var/* 2>/dev/null | sort -rh | head -5
14G     /var/lib
2.1G    /var/log
1.8G    /var/cache
420M    /var/spool
180M    /var/tmp

$ du -sh /var/lib/* 2>/dev/null | sort -rh | head -5
13G     /var/lib/docker
620M    /var/lib/apt
280M    /var/lib/mysql

Three levels deep and the culprit is already obvious: /var/lib/docker accounts for nearly the entire /var filesystem. One more hop confirms which Docker subsystem is responsible:

1
2
3
4
$ du -sh /var/lib/docker/* 2>/dev/null | sort -rh | head -5
9.8G    /var/lib/docker/overlay2
2.9G    /var/lib/docker/containers
310M    /var/lib/docker/volumes

overlay2 — image and container filesystem layers — dwarfs everything else, which is the same pattern docker system df would have shown directly. That’s the value of doing this manually once: it makes the docker system df shortcut make sense the next time, because you’ve seen with your own eyes which directory it’s summarising. Each du -sh dir/* | sort -rh invocation costs a few seconds on a tree this size; on a filesystem with millions of small files under overlay2 specifically, expect each pass to take noticeably longer, which is exactly the case where installing ncdu (even if it means freeing a few hundred megabytes first with apt clean) pays for itself.

The general pattern holds for any full filesystem: du -sh <dir>/* | sort -rh | head at the top level, then repeat inside whichever entry is largest, three or four hops deep is almost always enough to land on the actual offender rather than a parent directory that merely contains it.

The two-command habit worth keeping

df -h and df -i together, run first before reaching for anything else, answer “which filesystem, and is it actually a space or an inode problem” in about two seconds. ncdu on whichever filesystem comes back full answers “where” without the back-and-forth of manual du drilling. That’s genuinely the whole method — the only real skill in this is knowing to check -i and lsof +L1 before concluding the disk lied to you, because on the rare occasion it looks like it has, it almost never actually has.

Smarc
Written by Smarc

Founder and editor of Aside House. A lifelong tinkerer who self-hosts far more than is sensible, hardens Linux boxes for fun, and prods the latest AI tools to see what they can really do. The how-to guides here are the notes Smarc wishes had existed the first time round.

Smarc is the editor of Aside House. The homelab, the hardware and the opinions are real; the guides are drafted with AI assistance. Infrastructure details are genericised on purpose.