Table of Contents#
- Prerequisites
- Removing Docker Containers
- Removing Docker Images
- Removing Docker Volumes
- Bulk Cleanup: Remove All Containers, Images, and Volumes
- Conclusion
- References
Prerequisites#
Before starting, ensure:
- Docker is installed and running on your system (check with
docker --version). - You have basic familiarity with the command line.
- You have appropriate permissions (e.g.,
sudoon Linux/macOS, or run PowerShell as Administrator on Windows).
Removing Docker Containers#
What is a Docker Container?#
A container is a runnable instance of a Docker image—think of it as a lightweight, isolated environment for your application. Containers can be running, stopped, or exited, and each consumes disk space.
Step 1: List Containers#
First, identify which containers exist on your system. Use:
# List running containers
docker ps
# List all containers (running + stopped)
docker ps -aOutput example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:alpine "nginx -g 'd…" 2 hours ago Exited (0) 1 hour ago my-nginx
f7g8h9i0j1k2 ubuntu:20.04 "/bin/bash" 5 days ago Exited (127) 3 days ago old-ubuntu
Note the CONTAINER ID or NAMES (e.g., a1b2c3d4e5f6 or my-nginx) for the containers you want to remove.
Step 2: Stop Running Containers (If Needed)#
You cannot remove a running container unless you force it (see Step 5). To stop a running container:
docker stop <container_id_or_name>Example:
docker stop my-nginx # Stops the container named "my-nginx"Step 3: Remove Specific Containers#
To remove one or more stopped containers, use docker rm with the container ID or name:
# Remove a single container
docker rm <container_id_or_name>
# Remove multiple containers (space-separated IDs/names)
docker rm a1b2c3d4e5f6 f7g8h9i0j1k2Example:
docker rm my-nginx # Removes the "my-nginx" containerStep 4: Remove All Stopped Containers#
To clean up all stopped containers at once (safest bulk cleanup for containers):
docker rm $(docker ps -aq --filter "status=exited")docker ps -aq: Lists all container IDs (-afor all,-qfor quiet mode, only IDs).--filter "status=exited": Filters for stopped (exited) containers.
Step 5: Force Remove Running Containers#
If you need to remove a running container immediately (use with caution!), add the -f (force) flag:
docker rm -f <container_id_or_name>Example:
docker rm -f my-nginx # Stops and removes the running "my-nginx" containerStep 6: Prune Containers#
Docker provides a built-in prune command to remove all stopped containers (and optionally other resources). To prune containers:
docker container pruneYou’ll be prompted to confirm. To skip confirmation, add -f:
docker container prune -fRemoving Docker Images#
What is a Docker Image?#
An image is a read-only template containing instructions to create a Docker container. Images are stored locally and can be large, so cleaning unused images frees significant disk space.
Step 1: List Images#
To see all local images, run:
# List all images
docker images
# Or (newer syntax)
docker image lsOutput example:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 1234567890ab 2 weeks ago 23.5MB
ubuntu 20.04 abcdef012345 1 month ago 72.8MB
<none> <none> fedcba987654 3 months ago 1.2GB # Dangling image
Note the IMAGE ID (e.g., 1234567890ab) or REPOSITORY:TAG (e.g., nginx:alpine) for images to remove.
Step 2: Remove Specific Images#
To delete an image, use docker rmi (short for "remove image") with the image ID or repository:tag:
# Remove by image ID
docker rmi <image_id>
# Remove by repository:tag
docker rmi nginx:alpineExample:
docker rmi 1234567890ab # Removes the nginx:alpine imageStep 3: Force Remove Images in Use#
If an image is used by a running or stopped container, Docker will block deletion. To force-remove it (note: this will break dependent containers):
docker rmi -f <image_id_or_tag>Warning: Forcing image removal may leave containers in an invalid state. Always remove dependent containers first if possible.
Step 4: Remove Dangling Images#
"Dangling images" are untagged images (<none>:<none>) left behind when you rebuild images or pull newer versions. They serve no purpose and waste space. To remove them:
docker image pruneThis deletes all dangling images. Add -f to skip confirmation:
docker image prune -fStep 5: Prune Unused Images#
To remove all unused images (not just dangling ones), including those not associated with any container, use:
docker image prune -aThe -a flag includes all unused images (dangling + unused tagged images). Confirm with -f to skip prompts:
docker image prune -a -fRemoving Docker Volumes#
What is a Docker Volume?#
Volumes are persistent storage for containers, designed to survive container removal. They store data like databases, logs, or user uploads. Unlike containers/images, volumes are not automatically deleted, so manual cleanup is critical.
Step 1: List Volumes#
To list all volumes (named and anonymous):
docker volume lsOutput example:
DRIVER VOLUME NAME
local my-data-volume
local 8f9a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0 # Anonymous volume
Step 2: Remove Specific Volumes#
To delete a specific volume, use docker volume rm with the volume name:
docker volume rm <volume_name>Example:
docker volume rm my-data-volume # Removes the "my-data-volume" volumeNote: You cannot remove a volume that is currently mounted by a container. Stop/remove the container first.
Step 3: Prune Volumes#
To remove all unused volumes (not attached to any running container), use:
docker volume pruneConfirm with -f to skip prompts:
docker volume prune -fWarning: Pruning volumes deletes all data stored in them. Always back up critical data first!
Bulk Cleanup: Remove All Containers, Images, and Volumes#
For a full system cleanup (use with extreme caution!), you can combine commands to remove all containers, images, and volumes in one go:
# Stop all running containers
docker stop $(docker ps -aq)
# Remove all containers
docker rm $(docker ps -aq)
# Remove all images
docker rmi -f $(docker images -aq)
# Remove all volumes
docker volume rm $(docker volume ls -q)Alternatively, use Docker’s built-in system prune to clean everything (containers, images, networks, and optionally volumes):
# Prune containers, images, and networks (no volumes)
docker system prune -a
# Prune ALL resources (including volumes!)
docker system prune -a --volumesCritical Warning: docker system prune -a --volumes deletes all unused data, including volumes. Only use this if you’re sure you don’t need the data!
Conclusion#
Regularly cleaning Docker resources—containers, images, and volumes—keeps your system efficient and prevents disk bloat. Remember:
- Stop containers before removing them (unless forcing).
- Use
prunecommands for safe bulk cleanup. - Always back up volume data before deletion.
By following these steps, you’ll maintain a clean Docker environment and avoid unexpected storage issues.