# Docker Disk Space Cleanup

## Docker Disk Space Cleanup (Hosts)

Docker data grows over time on host machines.\
Old rentals leave behind stopped containers, unused images, volumes, and build cache.\
If you don’t clean it up, the root disk fills up and deployments start failing.

{% hint style="info" %}
Run cleanup only when the server is **not rented** and you don’t need any old container data.\
If you’re unsure, stop here and do a disk usage check first.
{% endhint %}

***

## 1) Check disk usage

### OS-level disk space (`df -h`)

This shows free space on each mounted filesystem.

```bash
df -h
```

You care mainly about `/` (root) and the partition that holds `/var/lib/docker`.

### Docker-level disk usage (`docker system df`)

This shows what Docker is storing and how much is reclaimable.

```bash
docker system df
```

If you want more detail per image/container:

```bash
docker system df -v
```

***

## 2) Complete cleanup (recommended)

This is the “reset Docker leftovers from previous rentals” command.\
It removes unused containers, images, networks, and **unused volumes**.

```bash
docker system prune -a --volumes
```

{% hint style="warning" %}
`-a` removes **all unused images**, not just “dangling” ones.\
That includes cached images you might want for faster future deployments.
{% endhint %}

***

## 3) Individual cleanup commands (more control)

Use these when you want to clean a specific category.\
They’re safer for incremental maintenance.

### Containers (stopped only)

```bash
docker container prune
```

### Images (unused)

Remove dangling layers only:

```bash
docker image prune
```

Remove all unused images (same risk as `system prune -a`):

```bash
docker image prune -a
```

### Volumes (unused)

```bash
docker volume prune
```

### Networks (unused)

```bash
docker network prune
```

***

## 4) Best practices for host maintenance

1. Clean up **between rentals** or during planned downtime.
2. Keep a safety buffer.\
   Aim for **10–20 GB** free on the root disk at all times.
3. Check Docker usage regularly:
   * `df -h`
   * `docker system df`
4. Prefer incremental cleanup first:
   * `docker container prune`
   * `docker image prune`
   * `docker volume prune`
5. Use full cleanup only when needed:
   * `docker system prune -a --volumes`
6. If disk usage keeps growing fast, investigate:
   * Large volumes created by renters.
   * Logs under `/var/lib/docker/containers/*/*.log`.
   * Build cache from frequent image rebuilds.
