Skip to content

Instantly share code, notes, and snippets.

@viannaandreBR
Forked from GhazanfarMir/clear-images.sh
Created March 10, 2022 03:56
Show Gist options
  • Save viannaandreBR/c925eff80ace35662d017dad6c366ff5 to your computer and use it in GitHub Desktop.
Save viannaandreBR/c925eff80ace35662d017dad6c366ff5 to your computer and use it in GitHub Desktop.
Single shell script to remove all containers, images and volumes used by containers. The script first tries to stop containers if there is any running, then remove the containers, followed by images removal and finally the container volumes.
#!/bin/bash
###########################################
#
# Simple Shell script to clean/remove all container/images
#
# The script will
# - first stop all running containers (if any),
# - remove containers
# - remove images
# - remove volumes
#
# stop all running containers
echo '####################################################'
echo 'Stopping running containers (if available)...'
echo '####################################################'
docker stop $(docker ps -aq)
# remove all stopped containers
echo '####################################################'
echo 'Removing containers ..'
echo '####################################################'
docker rm $(docker ps -aq)
# remove all images
echo '####################################################'
echo 'Removing images ...'
echo '####################################################'
docker rmi $(docker images -q)
# remove all stray volumes if any
echo '####################################################'
echo 'Revoming docker container volumes (if any)'
echo '####################################################'
docker volume rm $(docker volume ls -q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment