The basic command to remove a container

To delete a Docker container, use docker rm followed by the container name or ID. The container must be stopped before you remove it — if it is still running, Docker will refuse the command.

For example, if you have a stopped container named my-app, you would type:

docker rm my-app

Docker will return the container name if the deletion succeeded. If you do not know the container name, run docker ps -a to list all containers on your system, including stopped ones.

Key Takeaways

  • A container must be stopped before you can delete it; use docker stop first if the container is running.
  • You can remove a container by name or by its container ID, which you find with docker ps -a.
  • The -f flag forces deletion of a running container without stopping it first, but this can leave orphaned processes.
  • Deleting a container does not delete its image; you must use docker rmi separately to remove the image file.
  • Multiple containers can be deleted in one command by listing their names or IDs separated by spaces.

Stopping a running container before deletion

If a container is still running, Docker will return an error when you try to remove it. You must stop it first using docker stop followed by the container name or ID.

For example:

docker stop my-app

Docker will wait up to 10 seconds for the container to shut down gracefully. If the container does not stop within that time, you can use docker kill to force it to stop immediately, then delete it. After the container is stopped, the docker rm command will work.

Forcing deletion of a running container

If you want to skip the stop step, you can use the -f (force) flag to delete a running container in one command:

docker rm -f my-app

This tells Docker to kill the container and remove it without waiting for a graceful shutdown. The downside is that any processes inside the container are terminated abruptly, which can leave temporary files or incomplete transactions behind. For production containers or anything handling important data, stopping first is safer.

Removing multiple containers at once

You can delete several containers in a single command by listing their names or IDs separated by spaces:

docker rm container1 container2 container3

If some containers are running, add the -f flag to force removal of all of them:

docker rm -f container1 container2 container3

You can also use a pattern to match container names. For instance, docker rm $(docker ps -a -q) will delete every container on your system — use this carefully, as it cannot be undone.

The difference between removing a container and removing an image

Deleting a container does not delete the image it was created from. A Docker image is the template; a container is a running instance of that template. When you remove a container, the image remains on disk and can be used to create new containers.

If you want to remove the image itself, use docker rmi followed by the image name or ID. For example, docker rmi my-app:latest removes the image. You can only delete an image if no containers (running or stopped) are using it. If containers still reference the image, Docker will refuse the deletion unless you use the -f flag.

Cleaning up stopped containers automatically

Docker provides a cleanup command that removes all stopped containers, unused images, and dangling volumes in one operation:

docker system prune

This command is useful if you have accumulated many stopped containers over time. It will ask for confirmation before deleting anything. If you want to skip the confirmation, add the -a flag, but be aware that this also removes all unused images, not just dangling ones.

For a more targeted approach, docker container prune removes only stopped containers and leaves images untouched.

What happens to data inside a deleted container

When you delete a container, any data stored inside it is lost permanently unless you saved it elsewhere. Docker containers are designed to be temporary — data is not meant to persist after the container is removed.

If you need to keep data, you have two options before deletion: use docker cp to copy files from the container to your host machine, or use Docker volumes to store data outside the container so it survives deletion. Volumes are the recommended approach for databases, configuration files, or anything else you need to preserve.

Frequently Asked Questions

Can I recover a container after I delete it?

No. Once a container is deleted with docker rm, it cannot be recovered. The container's filesystem and any data inside it are permanently removed. If you need to preserve data, copy it out before deletion or use Docker volumes to store it separately.

What is the difference between docker stop and docker rm?

docker stop shuts down a running container but keeps it on disk. You can restart it later with docker start. docker rm deletes the container entirely. Stopped containers still take up disk space and appear in docker ps -a output.

Can I delete a container that has dependent containers?

Yes. Containers are independent of each other by default. Deleting one container does not affect others, even if they are linked or share a network. However, if you delete a container that other containers depend on for networking or data, those dependent containers will lose access to it.

Why does docker rm fail even though I stopped the container?

This usually means the container is still referenced by another container or by a service. Check if the container is part of a Docker Compose stack or a swarm service. If so, you must stop the stack or service first, not just the individual container. You can also use docker inspect to see what is holding a reference to it.

Does deleting a container delete the image it came from?

No. Images and containers are separate. Deleting a container leaves the image intact on your system. To remove the image, use docker rmi with the image name or ID. You can only delete an image if no containers are using it.