✦ For everyone, free.

Practical knowledge for real and everyday life

Home

19.2.7.3 Rm Multiple Containers

A focused guide to Rm Multiple Containers, connecting core concepts with practical Docker and container operations.

docker rm accepts more than one container identifier in a single invocation. You can pass any combination of container names and container IDs as space-separated arguments, and Docker will remove each one in sequence. This avoids running docker rm repeatedly for each container and is particularly useful when you know the exact set of containers you want to delete.

Basic Multi-Container Removal

docker rm container_one container_two container_three

Docker processes each identifier in order from left to right. For each container that is successfully removed, its name (or ID, depending on what you specified) is printed to standard output. If any container fails (for example, if it is still running), Docker prints the error for that container and continues to the next one.

Using Names and IDs Together

You can freely mix names and IDs in the same command:

docker rm web_server a1b2c3d4e5f6 db_container

Partial IDs that are unambiguous among all containers on the host also work:

docker rm a1b2 c3d4 e5f6

Removing All Stopped Containers at Once

A common pattern is to pass the output of docker ps -aq with a status filter to docker rm using command substitution:

docker rm $(docker ps -aq -f status=exited)
  • -a: Include all containers (not only running ones).
  • -q: Output only container IDs (quiet mode).
  • -f status=exited: Filter to containers that have exited.

This results in docker rm receiving all exited container IDs as arguments in a single command. On Windows PowerShell the syntax is the same:

docker rm $(docker ps -aq -f status=exited)

If there are no stopped containers, docker ps -aq returns nothing and docker rm is called with no arguments, which produces an error. To handle this safely in scripts, use docker container prune instead, or guard with a check:

STOPPED=$(docker ps -aq -f status=exited)
if [ -n "$STOPPED" ]; then
  docker rm $STOPPED
fi

Removing All Containers Regardless of State

To remove every container on the host, including running ones, combine docker ps -aq (without a status filter) with the --force flag:

docker rm -f $(docker ps -aq)

This force-kills all running containers before deleting them. Use only on hosts that are fully disposable, such as CI runners or local test environments.

Partial Failure Behavior

When removing multiple containers, docker rm does not stop processing if one fails. If container B is still running and containers A and C are stopped:

docker rm container_a container_b container_c

Output:

container_a
Error response from daemon: You cannot remove a running container b2c3d4e5...
container_c

Docker removes A and C, prints an error for B, and exits with a non-zero exit code because at least one operation failed. In scripts that treat any non-zero exit code as a failure, this can cause issues. Using --force avoids the error for running containers, or you can filter the list to stopped containers only.

Using docker container prune for All Stopped Containers

The preferred way to remove all stopped containers without manually constructing a container ID list is:

docker container prune

This prompts for confirmation and removes all containers with status exited, created, or dead. To skip the prompt:

docker container prune --force

The advantage of prune over command substitution is that it handles the empty-list case gracefully and provides a summary of reclaimed disk space.

Filtering with Labels

To remove multiple containers that share a specific label, first list them and then pass the IDs to docker rm:

docker rm $(docker ps -aq -f label=environment=test)

This removes all containers (stopped or with --force) that have the label environment=test.

Scripting Patterns

Remove containers created from a specific image:

docker rm $(docker ps -aq -f ancestor=nginx:latest)

Remove all containers whose name matches a pattern (using grep):

docker rm $(docker ps -aq --format "{{.Names}}" | grep "worker_")

Remove containers and their anonymous volumes:

docker rm -v container_one container_two

The -v flag removes anonymous volumes associated with each specified container. Named volumes are not affected.

Output When No Containers Match

If docker ps -aq with a filter returns nothing, the command substitution passes an empty string to docker rm. Docker treats this as an invocation with no arguments and outputs:

"docker rm" requires at least 1 argument.

Guard against this in automation by checking whether the container list is non-empty before calling docker rm, or by using docker container prune which handles the empty case without error.