How to Remove All Docker Images, Containers, Volumes, and Networks in 2024

How to Remove All Docker Images, Containers, Volumes, and Networks in 2024

Docker images are the cornerstone of your Docker environment, serving as templates from which containers are created. However, these images can accumulate over time, consuming valuable disk space and cluttering your system.

Removing Docker images and other redundant components is not just about freeing up space – it’s also a practice in maintaining a clean, efficient, and organized Docker environment.

This article will explain the various methods to remove all Docker images and related components. Whether you’re looking to free up disk space or declutter your Docker environment, this article provides the essential knowledge and practical steps to achieve it.

Download Free Docker Cheat Sheet

How to Remove All Unused Docker Images and Other Resources

Before clearing Docker images and resources, log in to your virtual private server (VPS) with an SSH client like PuTTY.

Using the Docker Image Prune Command

Unused Docker images are those not associated with any running or stopped containers. Here’s the basic command for pruning Docker images:

docker image prune

You’ll be prompted to confirm the removal. Once you approve, it will remove all dangling and unused Docker images.

Using the Docker System Prune Command

The docker system prune command removes unused images, containers, networks, and the build cache. Run the following Linux command to delete these unused resources:

docker system prune

For a more thorough cleanup that includes volumes, use:

docker system prune -a --volumes

Warning! Note that this command will purge Docker images, stopped containers, unused networks, and the build cache. Proceed with caution when using this command, as the action is irreversible.

How to Remove Docker Images

Before performing any Docker image cleanup, identify the image’s ID. To list all the Docker images in your system along with their IDs, execute:

docker images

If you are using a more recent version of Docker on your local machine, employ the docker image ls command instead.

How to Remove a Single Docker Image

Removing a single Docker image is simple, but make sure the correct image is being deleted.

Using the Docker RMI Command

The primary command for removing a single Docker image is docker rmi, where RMI stands for remove image.

For example, to remove an image with the ID a2a15febcdf3, use the following command:

docker rmi a2a15febcdf3

If the specified image is in use by an existing container, the command will return an error, preventing accidental deletion.

The docker rmi command can also work with the repository name and tag if you don’t have the image ID. For instance:

docker rmi ubuntu:latest

This command removes the ubuntu image with the latest tag.

Using the Docker Image RM Command

The docker image rm command works similarly and can be used interchangeably with docker rmi. For instance:

docker image rm a2a15febcdf3

How to Remove Multiple Images

In specific scenarios, you might need to remove multiple Docker images simultaneously. Docker provides a simple way to handle this.

Removing Multiple Docker Images

To remove multiple Docker images, use the docker rmi command followed by the image IDs you want to delete.

For instance, to remove images created with the IDs a2a15febcdf3 and 4bb46517cac3, use the following command:

docker rmi a2a15febcdf3 4bb46517cac3

Removing All Docker Images

If you need to remove all the images on your system, use the following command:

docker rmi $(docker images -q)

The docker images -q command lists your Docker image IDs. Using $() with docker rmi removes all your Docker images.

If an image is in use by a container, the command will fail for that specific image but will continue removing the others.

How to Remove Dangling Images

A dangling image occurs when a new build replaces an older image, making the previous one redundant.

Although dangling images don’t pose a direct problem, they take up unnecessary disk space. Therefore, it’s good practice to remove these images periodically.

To identify dangling images, use the docker images command with the dangling=true filter as shown below:

docker images -f "dangling=true"

Then, you can remove these dangling images with the docker image prune command.

How to Remove Images Based on a Pattern

You can remove an image in Docker based on a specific naming pattern or tag.

Using Patterns to Identify Specific Images

To remove Docker images based on a pattern, combine the docker images command with the grep command to filter out the targeted images.

For instance, to list all images belonging to myproject, use the following command:

docker images | grep 'myproject'

Removing Images Based on the Filtered Pattern

Once you have identified the specific images, delete them by combining commands. Here’s an example of deleting Docker images with myproject in their name:

docker rmi $(docker images | grep 'myproject' | awk '{print $3}')

The grep ‘myproject’ command filters the images, while awk ‘{print $3}’ extracts their image IDs. The resulting image ID list is then passed to docker rmi for removal.

How to Remove Docker Containers

Containers are instances of Docker images running as processes. Before removing any Docker container, list the containers in your system with the docker ps command.

docker ps

Note that this command only shows running containers. To see all containers, including those that have stopped, add the -a flag:

docker ps -a

How to Remove a Specific Container

You can delete unused containers after identifying the ones you wish to remove by their container IDs.

Using the Docker RM Command

The docker rm command is the primary tool for removing a specific container. For example:

docker rm 4e5021d210f6

Replace 4e5021d210f6 with your actual container ID.

Forcefully Removing a Running Container

If you have Docker containers running in your system, use the following command to stop the container and then clear it:

docker rm -f 4e5021d210f6

Alternatively, you can stop the container with the docker stop command and remove it using docker rm.

How to Remove All Stopped Containers

Stopped containers are ones that have completed their tasks or have been terminated manually. To remove all stopped containers, use the docker container prune command as follows:

docker container prune

This command cleans up and removes all the containers with a stopped status.

Using the –filter flag, you can selectively remove stopped containers within a specific time frame. For example:

docker container prune --filter "until=24h"

How to Remove Containers Upon Exiting

Setting containers to automatically remove themselves upon exiting is useful in environments with frequent container creation and destruction, such as for testing.

To do so, include the –rm flag when running the container using the docker run command. For example:

docker run --rm <your_image>

In this command, replace <your_image> with the intended image name.

How to Remove Docker Volumes

Volumes store persistent data generated and used by Docker containers. Before removing any Docker volume, identify it first using the docker volume ls command:

docker volume ls

How to Remove a Certain Volume

Once you have identified the volume you wish to remove, use the docker volume rm command followed by the volume’s name. Here’s an example:

docker volume rm my_volume

How to Remove All Unused Volumes

To remove all unused Docker volumes and reclaim valuable disk space, execute the following command:

docker volume prune

How to Remove Volumes With Attached Labels

Label-specific volumes are those assigned to one or more labels. These labels help target removal in a specific project, environment, or other categories.

Finding and Removing Volumes by Labels

To list all volumes with a specific label, use the docker volume ls command with a filter based on the label. Here’s an example:

docker volume ls -f "label=project=myproject"

Once you identify the volumes you wish to remove, delete them individually with the docker volume rm command since Docker prohibits removing multiple volumes with a single command.

Automating Volume Removal Based on Labels

For a more automated approach, use command-line tools like xargs. The command below combines listing and removing volumes based on a label:

docker volume ls -qf "label=project=myproject" | xargs docker volume rm

This command lists all volume names with the project=myproject label and then passes them to docker volume rm for removal.

How to Remove Docker Networks

Docker networks enable communication between containers. To list all the Docker networks on your system along with their IDs, execute:

docker network ls

How to Remove a Specific Network

To remove a specific Docker network, use the docker network rm command followed by the network’s ID or name. For example:

docker network rm 3cf8fbf4f034

Alternatively, use the network’s name as shown below:

docker network rm mynetwork

How to Remove All Unused Networks

To remove all unused Docker networks, execute this command:

docker network prune

Warning! Executing this command will permanently delete any active configurations or settings related to these networks.

How to Remove Networks With a Specific Timeframe

To remove Docker networks within a specific timeframe, use the docker network prune command with the –filter option. Here’s an example:

docker network prune --filter "until=24h"

You can apply multiple filters simultaneously. For example, to remove networks that haven’t been used in the last 30 days (720 hours), run:

docker network prune --filter "unused=true" --filter "until=720h"

Best Practices for Removing Images in Docker

Applying best practices for Docker image removal is essential to ensure your environment is clutter-free. It also helps safeguard against accidental data loss.

Use Hostinger VPS AI Assistant

Efficiency and accuracy are vital when managing a Docker environment. Hostinger’s VPS AI Assistant offers a streamlined approach to optimize Docker image storage.

Available with all Hostinger VPS hosting plans, this AI Assistant excels at Docker-related queries. It can provide step-by-step guides, commands, and scripts for efficient Docker image removal.

To use the Hostinger VPS AI Assistant, follow these steps:

  1. Log in to your Hostinger account.
  2. Locate the VPS dashboard and select your server.
  3. Navigate to AI Assistant from the left panel.

Enter your query about Docker image management, such as “How do I remove unused Docker images?” The AI Assistant will respond with a comprehensive answer, including any relevant commands or scripts.

Demonstrating the use of AI Assistant to ask about removing unused Docker images

Suggested Reading

Discover how to use Hostinger’s AI Assistant effectively with the best AI prompts for efficient VPS management.

Tag and Version Your Docker Images

Properly tagging and versioning are crucial for streamlining Docker image storage. Here are some effective strategies for image tagging:

  • Use meaningful tags – instead of using vague tags like latest, opt for more descriptive ones. For instance, myapp:2.5.1 denotes the 2.5.1 version of the myapp image.
  • Tag image ID with version – when building an image, tag it with a meaningful version number. For example, use docker build -t myimage:1.0 to tag your image as version 1.0.
  • Semantic versioning – adopt a semantic versioning scheme, like major.minor.patch, for clarity and consistency. For example, an update that fixes a bug might increment the patch number, changing the version to myapp:2.5.2.

Back Up Important Images

Backing up images is vital to ensure they are safely stored and easily retrievable in case of accidental deletion or unforeseen issues.

There are several methods to create Docker image backups:

  • Exporting images – Docker lets you export images to a tarball format, which can then be securely stored. This provides an offline copy of your image, available to be reloaded into Docker when needed.
  • Using the Docker save command – to export an image, use the docker save command. For instance, to save an image named myapp:latest, execute docker save myapp:latest > myapp_latest.tar.
  • Choosing a secure location – storing your exported image tarballs in a safe and reliable environment is essential. Options include cloud storage, an external hard drive, or any other off-site solution that is secure and accessible.

Review Image Dependencies

Image dependencies refer to the interrelations among various Docker images. Removing an image without considering its dependencies may result in non-functional containers or data loss within linked volumes.

To identify and manage image dependencies, consider the following strategies:

  • Document image relationships – maintain clear and updated documentation on your Docker images and their interrelationships. This lets you understand the dependencies at a glance.
  • Use tagging strategically – strategically use tags to indicate critical images or dependencies. This helps prevent deleted images due to accidents.
  • Regular dependency checks – conduct periodic reviews of your Docker environment for any changes in dependencies, especially before undertaking major cleanups or updates.


In this guide, we’ve explored various methods for cleaning up Docker images and other components.

We’ve also highlighted several best practices, such as utilizing tools like Hostinger’s VPS AI Assistant and performing regular Docker image reviews and documentation to avoid accidental data loss.

By implementing these best practices and techniques, you can manage your Docker images and containers more effectively, ensuring a cleaner, more efficient, and reliable system.

The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.