A Docker container stops running when you tell it to, but the container itself stays on your computer until you delete it
When you stop a container, Docker keeps it around — the files, the settings, everything. This is useful if you want to restart it later without rebuilding it. But if you are done with a container for good, you need to remove it to free up disk space and keep your system clean. Deleting a container is straightforward: you use the docker rm command with the container's name or ID.
The key thing to understand is that you cannot delete a container while it is still running. You have to stop it first, then remove it. Or you can force the removal in one step if you are certain you do not need the container again.
Key Takeaways
- A stopped container still exists on your computer and takes up disk space until you delete it with docker rm.
- You must stop a container before removing it, unless you use the -f flag to force removal while it is running.
- You can delete a container by its name or by its container ID, which you can find with docker ps -a.
- Deleting a container does not delete the image it was built from, so you can create new containers from that image later.
- If you want to delete all stopped containers at once, use docker container prune to clean up without naming each one.
Stop the container before you delete it
A running container must be stopped before Docker will let you remove it. Open your terminal and run docker stop followed by the container name or ID. For example, if your container is named web-server, type:
docker stop web-server
Docker will shut down the container gracefully, giving it a few seconds to finish what it is doing. The container now exists in a stopped state — it is no longer using CPU or memory, but it is still stored on your disk.
Remove the container with docker rm
Once the container is stopped, use docker rm to delete it. Type:
docker rm web-server
Docker will remove the container completely. The command produces no output if it succeeds — that silence means the deletion worked. If you see an error saying the container does not exist, you may have already deleted it or used the wrong name.
You can also delete a container by its ID instead of its name. Run docker ps -a to see a list of all containers (running and stopped). Each one has a unique ID in the first column — a long string of letters and numbers. You can use the first 12 characters of that ID in place of the name:
docker rm a1b2c3d4e5f6
Force delete a running container if you need to
If you want to delete a container without stopping it first, use the -f flag to force removal:
docker rm -f web-server
This stops and deletes the container in one command. Use this only when you are certain you do not need the container anymore, because there is no confirmation step. The container and any data inside it will be gone when ready.
Forcing removal is useful when a container is stuck or not responding to the normal stop command. But in normal work, stopping first and then removing is safer because it gives you a moment to reconsider.
Find container names and IDs with docker ps
If you do not remember the name of the container you want to delete, use docker ps -a to list all containers on your system:
docker ps -a
This shows every container, including ones that are stopped. The output includes the container ID, the image it was built from, when it was created, its current status, and the name you gave it (or the name Docker assigned if you did not specify one). Find the container you want to delete and note its name or ID.
If you only want to see running containers, use docker ps without the -a flag. Stopped containers will not appear.
Delete multiple containers or clean up all stopped ones at once
If you have several containers to delete, you can remove them all in one command by listing their names or IDs:
docker rm web-server database-server cache-server
For a faster cleanup, use docker container prune to delete all stopped containers at once:
docker container prune
Docker will ask you to confirm before deleting. This is useful when you have accumulated many stopped containers and want to reclaim disk space without naming each one. Running containers are never deleted by prune — only stopped ones.
Understand what gets deleted and what stays
Deleting a container removes the container itself — the running instance, its files, and any data stored inside it. But it does not delete the image that the container was built from. If you delete a container and later want to create a new one from the same image, you can do so without rebuilding the image.
For example, if you created a container named web-server from the image nginx:latest, deleting the container does not delete the nginx:latest image. You can create a new container from that image anytime. If you want to delete the image itself, use docker rmi — but that is a separate step.
Data stored inside a container is lost when the container is deleted. If you need to keep that data, you should have copied it out or used a Docker volume to store it outside the container before deleting.
Frequently Asked Questions
What happens to data inside a container when I delete it?
All data stored inside the container is deleted along with it. If you need to keep files or a database, copy them out before deletion or use a Docker volume, which stores data outside the container on your computer's disk.
Can I recover a deleted container?
No. Once you run docker rm, the container is gone permanently. You can create a new container from the same image, but it will start fresh with no data from the old one.
Does deleting a container delete the image too?
No. The image remains on your computer. You can create new containers from it anytime. To delete an image, use docker rmi with the image name or ID.
Why would I keep a stopped container instead of deleting it?
A stopped container preserves its configuration and any data inside it. If you might restart it later, keeping it saves you from rebuilding it. But if you are done with it, deletion frees up disk space.
What is the difference between docker stop and docker rm?
docker stop shuts down a running container but leaves it on your disk. docker rm deletes the container entirely. You usually stop first, then remove — or use docker rm -f to do both at once.