Pruning Networks

Difficulty: Beginner

Question

How do you remove all unused Docker networks, and what exactly counts as 'unused'?

Answer

Run docker network prune, confirm the prompt (or pass -f), and Docker deletes every user-defined network that has no containers attached to it. The three built-in networks, bridge, host and none, are never removed, and any network with even one container connected (running or stopped) is left alone.

What prune looks at

A network is a prune candidate when it was created by a user or by Compose and docker network inspect shows no attached endpoints. A stopped container still holds its network attachment, so its network survives the prune. If you want those networks gone too, remove the containers first (docker container prune) or run docker system prune, which removes stopped containers, unused networks, dangling images and dangling build cache in one go.

To delete a specific network instead, use docker network rm <name>. It fails with "has active endpoints" if a container is still connected, which is a useful safety net.

Why bother cleaning up networks

Networks take almost no disk space, so this is not about storage. The real reasons are:

  • Subnet exhaustion. Every bridge network grabs a subnet from Docker's default address pools. Developers who run many Compose projects can eventually hit "could not find an available, non-overlapping IPv4 address pool" until stale networks are removed.
  • Address clashes with corporate VPNs. Leftover networks in ranges such as 172.17.0.0/16 to 172.31.0.0/16 or 192.168.x.x can overlap with an office VPN and break routing on a laptop.
  • Clean CI runners. Self-hosted runners that create a network per pipeline accumulate hundreds of them over a week.

Filters for safer cleanup

FilterExampleEffect
until--filter "until=24h"Only networks created more than 24 hours ago
label--filter "label=env=ci"Only networks carrying that label
label!--filter "label!=keep"Skip networks labelled keep

On a shared CI host, combining until with a label set by your pipeline means one team's cleanup never removes a network another job is about to use.

Common mistakes

  • Expecting prune to fix a running Compose stack. docker compose down already removes the project's default network; prune is for leftovers from crashed or abandoned runs.
  • Running it in production cron without filters. A network created a second ago by a deploy script, before its container attached, is technically unused and can be removed out from under the deploy.
  • Forgetting Swarm. On a Swarm manager, overlay networks without service tasks are also candidates, so be deliberate there.

Code examples

Inspect, then prune with a safety filter

# See which networks exist and how many containers each one has
docker network ls
docker network inspect myapp_default --format '{{len .Containers}} containers attached'

# Remove all unused user-defined networks (asks for confirmation)
docker network prune

# Non-interactive, only networks older than a day, skip ones labelled keep=true
docker network prune -f --filter "until=24h" --filter "label!=keep=true"

The warning text spells out the rule: custom networks with no container. The filters make the command safe enough for a scheduled job on a shared host.

Key points

  • docker network prune removes user-defined networks with no attached containers
  • bridge, host and none are never pruned
  • A stopped container still keeps its network in use
  • Use --filter until= and label filters to make automated cleanup safe
  • docker system prune also removes unused networks along with stopped containers and dangling images

Common follow-up questions

  • What is the difference between the default bridge network and a user-defined bridge?
  • How do you change Docker's default address pools to avoid VPN conflicts?
  • What does docker system prune remove, and what does it leave behind?

Interview tips

  • State precisely what 'unused' means, including the stopped-container case; that detail separates you from a one-line answer
  • Mention subnet exhaustion as the practical reason to prune, since disk space is not

Concepts covered

docker network prune, User-defined networks, Default networks, Prune filters, Disk and IP cleanup

More Docker & Containers questions