Home / Alt manpages / docker-network-rm(1)

  • docker-network-rm(1)
  • User command
  • linux

Remove Docker Networks Without Losing the Wrong One

You will remove a Docker network by name or ID, check that it has really gone, and handle the usual attached-container failure without guessing. The examples use Docker CLI 29.8.1, installed from docker-ce-cli version 5:29.8.1-1~ubuntu.24.04~noble.

Allow about ten minutes. You need a shell, the Docker CLI, access to the Docker daemon, and a network that you have permission to remove. The command changes Docker state and network connectivity. It normally does not need sudo when your user can already run Docker, but use your system's approved privilege method if the daemon denies access.

1. Check the command before changing state

Read the installed command's short contract first:

$ docker network rm --help
Usage:  docker network rm NETWORK [NETWORK...]

Remove one or more networks

Options:
  -f, --force   Do not error if the network does not exist

The positional argument is one or more network names or IDs. The only option is --force, and it only changes what happens when a named network does not exist. It does not disconnect containers and it does not override a network that is still in use.

Checkpoint: confirm the client and list the networks before selecting a target:

$ docker --version
Docker version 29.8.1, build 4a63305
$ docker network ls

Record the exact name or ID you intend to remove. Do not copy a row from docker network ls without checking its name and purpose. A network ID prefix can identify a network, but a name is easier to review in a shell history.

2. Inspect the target and its attachments

Before deletion, inspect the network:

$ docker network inspect NETWORK_NAME

Replace NETWORK_NAME with the real name or ID. In the JSON output, look for the Containers object. Its entries show containers connected to the network. This is a read-only check and does not require elevated privileges beyond the Docker access already needed by the CLI.

Do not treat an empty Containers object as proof that the network is unimportant. It can still be referenced by deployment files, scripts or a service that has not started yet. Also avoid removing Docker's built-in networks such as bridge, host and none as part of ordinary cleanup. Select a user-created network deliberately.

3. Disconnect containers before removing the network

Removing a network that still has connected containers fails. If the attached containers should remain, disconnect them individually first:

$ docker network disconnect NETWORK_NAME CONTAINER_NAME

Repeat the command for each attached container. Disconnecting is service-disrupting: processes in the container can lose network access immediately, although the container itself is not removed. If the container needs another network, connect it before disconnecting the old one, then verify its routes and application health:

$ docker network connect REPLACEMENT_NETWORK CONTAINER_NAME
$ docker network disconnect NETWORK_NAME CONTAINER_NAME
$ docker inspect --format '{{json .NetworkSettings.Networks}}' CONTAINER_NAME

There is an undo for a mistaken disconnect: reconnect the container with docker network connect NETWORK_NAME CONTAINER_NAME. That restores membership, but it cannot recreate application-level connections that were interrupted.

4. Remove one network

After checking the target and disconnecting anything that should remain elsewhere, remove it:

$ docker network rm NETWORK_NAME
NETWORK_NAME

Successful output contains the removed network name or ID. The command removes the network object; it does not remove containers, images or volumes. Treat this as destructive because Docker does not provide a general restore command for a removed network. Keep the network's configuration, subnet and gateway details if you may need to recreate an equivalent network later.

Checkpoint: ask Docker for the same object and expect a failure:

$ docker network inspect NETWORK_NAME
Error response from daemon: network NETWORK_NAME not found

The exact error wording can vary, but a non-zero result saying that the network cannot be found is the useful verification. If the inspect command still succeeds, stop: you may have removed a different network or used a different name.

5. Remove several networks with per-item results

You can provide several names or IDs in one command:

$ docker network rm PROJECT_FRONTEND PROJECT_BACKEND
PROJECT_FRONTEND
PROJECT_BACKEND

Docker attempts each network in turn. If one removal fails, it continues to the next and reports success or failure for each item. Do not assume that one successful line means the whole batch succeeded. Verify each intended target:

$ docker network inspect PROJECT_FRONTEND >/dev/null 2>&1; printf 'frontend status: %s\n' "$?"
$ docker network inspect PROJECT_BACKEND >/dev/null 2>&1; printf 'backend status: %s\n' "$?"
frontend status: 1
backend status: 1

For a larger cleanup, put reviewed network names in a file and process them with a script that records each exit status. Do not generate the list from broad text matching unless you have checked the result first. A loose pattern can include a live network belonging to another project.

6. Use force only for an already absent network

If a cleanup step should be idempotent, --force prevents an error when the named network does not exist:

$ docker network rm --force NETWORK_NAME
NETWORK_NAME

When the network is absent, Docker accepts the request without reporting that absence as an error. This is useful in teardown scripts that may be run twice. It is not a confirmation that a network was removed, and it does not make an attached network safe to delete. Check the target first when an unexpected name would indicate a deployment problem.

Keep --force out of an interactive command when you are still identifying the target. It can hide a spelling mistake or a stale inventory entry. For a script, log the name being processed and treat other failures, such as an attached container or daemon outage, as actionable.

Common failures and recovery

Network has active endpoints: inspect the network, identify the containers, and decide whether to connect them elsewhere before disconnecting them. Do not stop or remove containers merely to make the network deletion pass.

Network not found: check spelling with docker network ls. Use --force only when absence is an acceptable final state.

Permission denied or daemon unavailable: confirm that Docker is running and that your account has the expected daemon access. Fix that access through normal administration; do not weaken socket permissions as a quick workaround.

Wrong network removed: there is no generic Docker undo. Recreate the network from its recorded driver and options, reconnect the intended containers, and verify their service configuration. This is why inspection and a reviewed target matter before the removal command.

Done means

  • The target name or ID was checked against docker network ls and docker network inspect.
  • Connected containers were deliberately kept, moved or disconnected before removal.
  • docker network rm reported the intended result for every requested network.
  • Each removed network now fails the inspect check, and no unrelated network was changed.
  • --force was used only where an already absent network is an acceptable outcome.