Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Remove Docker Images, Volumes and Containers

Free disk space by removing the Docker images, volumes and containers you no longer need.

VPS and Cloud Servers2 min read15 screenshots

Prepare some test items

Run a test container and create an image and a volume:

sudo docker run hello-world
Running the hello-world container
sudo docker pull ubuntu:latest
Pulling the Ubuntu image
sudo docker volume create my-volume
sudo docker run -v my-volume:/app/data ubuntu:latest
Creating a volume and using it
sudo docker run -it ubuntu:latest /bin/bash
Running an interactive container

Remove images

List the images:

sudo docker images
Listing the Docker images

Remove an image by ID:

sudo docker rmi <image_id>

Example:

sudo docker rmi d2c94e258dcb
Removing an image by ID

Remove an image by name:

sudo docker rmi ubuntu:latest
Removing an image by name

An image used by a container cannot be removed until you remove the container (or use -f to force it).

Remove all dangling (untagged, unused) images:

docker image prune
Pruning the unused images

Remove all images that no container uses:

docker image prune -a

Remove volumes

List the volumes:

sudo docker volume ls
Listing the Docker volumes

Remove one volume:

sudo docker volume rm my-volume
Removing a volume

Remove all volumes that no container uses:

sudo docker volume prune
Pruning the unused volumes
Warning: Volumes hold data such as databases. A removed volume cannot be recovered.

Remove containers

List the running containers and all containers:

sudo docker ps
sudo docker ps -a
Listing the containers

Stop a running container:

sudo docker stop <container_id>
Stopping a container

Remove it:

sudo docker rm <container_id>
Removing a container

Remove all stopped containers:

sudo docker container prune
Pruning the stopped containers

Clean everything at once

sudo docker system prune

It removes stopped containers, unused networks and dangling images. Add -a to remove all unused images and --volumes to remove unused volumes too. Check the use of disk space first with:

sudo docker system df

Quick recap

  • docker rmi, docker volume rm and docker rm remove single items.
  • docker system prune cleans many at once. Use it carefully.