Skip to content

Ubuntu Disk Cleanup Guide (Docker + KVM/libvirt)

Last reviewed: 2026-09-17

Purpose: Command-level walkthrough for reclaiming disk space on an Ubuntu host, covering Docker artefacts, KVM/libvirt VM storage, journal logs, caches and the tools that make the investigation quick.


Overview

This guide helps you quickly identify and clean up disk space on an Ubuntu system, focusing on:

  • Docker
  • KVM / libvirt virtual machines
  • System logs

For a broader discussion of strategy and risk, see Disk-Cleanup.md.


1. Identify Disk Usage

Check top-level disk usage

sudo du -h / --max-depth=1 | sort -hr

Drill into /var

sudo du -h /var --max-depth=1 | sort -hr

Drill into /var/lib

sudo du -h /var/lib --max-depth=1 | sort -hr

2. Docker Cleanup

Check Docker size

sudo du -sh /var/lib/docker

Clean unused resources

sudo docker system prune -a -f

Clear container logs

sudo truncate -s 0 /var/lib/docker/containers/*/*.log

Limit future log growth

Edit:

sudo nano /etc/docker/daemon.json

Add:

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

Restart Docker:

sudo systemctl restart docker

3. KVM / libvirt Disk Management

Check VM disk usage

sudo du -h /var/lib/libvirt/images | sort -hr | head -20

List virtual machines

virsh list --all

Delete unused VM (frees space immediately)

virsh undefine <vm-name> --remove-all-storage

This permanently deletes the VM and its disk.

Inspect disk usage (important)

qemu-img info /var/lib/libvirt/images/<file>.qcow2

Key fields:

  • virtual size β€” maximum allocated size
  • disk size β€” actual used space

Interpretation:

Case Meaning
disk size β‰ˆ virtual size Disk is full (no easy recovery)
disk size << virtual size Can reclaim space

Compact a VM disk

Step 1: Clean inside the VM

df -h
du -h / --max-depth=1 | sort -hr

Remove unnecessary files.

Step 2: Zero free space

dd if=/dev/zero of=zero.fill bs=1M
rm zero.fill

Step 3: Compact the image

qemu-img convert -O qcow2 old.qcow2 compacted.qcow2

Replace the original file after verification.

Find orphaned disks

ls /var/lib/libvirt/images
virsh list --all

Delete disks that are not attached to any VM, after verification.


4. System Logs Cleanup

Check journal size

journalctl --disk-usage

Clean logs (time-based)

sudo journalctl --vacuum-time=7d

Clean logs (size-based)

sudo journalctl --vacuum-size=500M

5. General Cleanup Targets

Logs

sudo du -sh /var/log

Cache

sudo du -sh /var/cache

Clean:

sudo apt clean

Snap cleanup

snap list --all

Remove old revisions:

sudo snap remove <package> --revision=<old_revision>

6. Useful Tools

ncdu (interactive disk analyzer)

sudo apt install ncdu
sudo ncdu /

The quickest way to spot large files and directories interactively.


7. Quick Cleanup Script

# Docker logs
sudo truncate -s 0 /var/lib/docker/containers/*/*.log

# Docker unused data
sudo docker system prune -a -f

# System logs
sudo journalctl --vacuum-time=7d

8. Important Warnings

  • Do not delete /var/lib/docker manually
  • Do not delete .qcow2 files blindly
  • Always verify before removing VMs
  • Use virsh for VM management

  1. Identify large directories (du, ncdu)
  2. Check /var/lib
  3. Investigate Docker and libvirt
  4. Remove unused VMs or data
  5. Clean logs and caches
  6. Prevent future growth (log rotation)

Topics Covered

  • Diagnosing disk usage with du and ncdu
  • Docker image, container, volume and build-cache cleanup, plus log rotation
  • KVM/libvirt VM inspection, removal, orphaned-image detection and qcow2 compaction
  • Journal and system-log vacuuming, apt and snap cache cleanup
  • A safe, ordered cleanup workflow with the operations that must not be done casually