Contents

Docker rm Tutorial: Remove Containers with -f, -v, and prune

docker rm removes stopped containers. Use -f to forcibly remove a running container. See Container lifecycle for the full process of creating, stopping, and removing containers.

Syntax

docker rm [OPTIONS] CONTAINER [CONTAINER...]

Options

OptionDescription
-f, --forceForce-remove a running container (uses SIGKILL)
-l, --linkRemove the specified link
-v, --volumesAlso remove anonymous Volumes associated with the container
--filter CONDITIONFilter containers, for example, until=<timestamp>

docker container prune removes all stopped containers; -f skips the confirmation prompt.

Examples

Remove stopped and running containers

$ docker rm app
Error response from daemon: cannot remove container "app": container is running: stop the container before removing or force remove

$ docker rm -f app
app

$ docker rm job
job
  • You can't remove the running app directly. The error message says to stop the container or force-remove it.
  • docker rm -f app successfully removes the running container and outputs its name, app.
  • You can remove the stopped job directly without adding -f.

Remove an anonymous Volume too

$ docker volume ls -q | wc -l
2

$ docker rm -f -v withvol
withvol

$ docker volume ls -q | wc -l
1
  • There are 2 Volumes before removal. After removing withvol with -v, 1 remains.
  • Use -v to remove anonymous Volumes associated with a container.

Clean up all stopped containers

$ docker ps -a --format '{{.Names}} {{.Status}}'
old2 Exited (0) Less than a second ago
old1 Exited (0) 1 second ago
registry Up About a minute

$ docker container prune -f
Deleted Containers:
1634aa3747102ed4016d3695d9fbe33f9ff7310fde9c5726ef86a340f8d22acb
a3ec6e520acc56ef9f9d78452b104675b7afd070a8e9bd1ab0a010b34e2fe8a5

Total reclaimed space: 8.192kB
  • Before removal, there are two stopped containers, old2 and old1, and one running container, registry.
  • docker container prune -f skips the confirmation prompt and outputs the IDs of the removed containers and the reclaimed space.
  • This command removes stopped containers. The running registry is not included in the removal list.

Command substitution returns no results

$ docker rm $(docker ps -aq --filter status=exited)
docker: 'docker rm' requires at least 1 argument

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

See 'docker rm --help' for more information
  • When docker ps -aq --filter status=exited returns no output, command substitution passes no container arguments.
  • As a result, docker rm reports that at least one argument is required. Use docker container prune -f to clean up containers in bulk.

Automatically remove a container when it exits

$ docker run --rm alpine:3.23 echo 'removed when done'
removed when done

$ docker ps -a --format '{{.Names}} {{.Status}}'
registry Up About a minute
  • --rm automatically removes a container when its command finishes.
  • The subsequent container list shows only the running registry; it doesn't list the container that just finished.

Exercises

Exercise: Force-remove the running app container and remove its associated anonymous Volume too.

Answer
docker rm -f -v app

FAQ

Why can't docker rm remove a running container?

A running container must be stopped first, or forcibly removed with docker rm -f; -f uses SIGKILL.

How do you remove an anonymous Volume along with a container?

Add -v, for example, docker rm -f -v withvol. In the example, the number of Volumes drops from 2 to 1.

What happens if command substitution finds no stopped containers?

docker rm $(docker ps -aq --filter status=exited) reports that at least one argument is required because no container name or ID was passed.

How do you automatically remove a container when it exits?

Run docker run --rm; after the container in the example finishes, docker ps -a no longer lists it.

Updated October 26, 2026