Difficulty: Beginner
How do you remove all unused Docker networks, and what exactly counts as 'unused'?
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.
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.
Networks take almost no disk space, so this is not about storage. The real reasons are:
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.| Filter | Example | Effect |
|---|---|---|
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.
docker compose down already removes the project's default network; prune is for leftovers from crashed or abandoned runs.# 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.
docker network prune, User-defined networks, Default networks, Prune filters, Disk and IP cleanup