
Docker cleanup has three separate targets: containers, images, and volumes. Removing a container does not automatically remove a named volume, and deleting a volume can permanently destroy application data.
Identify what you plan to remove
docker ps -a
docker image ls
docker volume lsRecord the exact name or ID before running a removal command.
Remove a stopped container
docker rm my-containerThis removes the container object. It does not remove the image used to create it or a named volume attached to it.
To remove several stopped containers, review the list first:
docker container pruneDocker asks for confirmation and removes stopped containers only.
Remove an image
docker image rm nginx:1.27-alpineDocker may refuse if a container still references the image. Remove or replace those containers first. Do not use --force until you understand which tags and image references will be affected.
Remove a volume
docker volume inspect my-data
docker volume rm my-dataWarning: A volume can contain databases, uploads, or other persistent data. Back it up and confirm the name before docker volume rm or docker volume prune.
Docker will not remove a volume that is still in use. Stop and remove the relevant containers first, then check the volume again.
Prune unused objects
docker image prune
docker container prune
docker volume pruneThese commands target specific unused object types. Read the confirmation prompt carefully. docker system prune combines several cleanup operations, so it deserves extra caution.
Compose cleanup
docker compose downThis stops and removes the project’s containers and networks. It does not remove named volumes by default. Adding -v removes the Compose project’s named volumes too:
docker compose down -vImportant: Use docker compose down -v only when you intentionally want to delete the stored data.
Safe cleanup checklist
- List containers, images, and volumes.
- Identify whether data is stored in a named volume or bind mount.
- Back up important data.
- Remove the container first.
- Remove the image or volume only when you are certain it is unused.
Official references
See Docker’s container removal reference, volume documentation, and pruning guide.