Docker – Shell Script to Remove Docker Images

Following script could help remove all of the docker images starting with a specific prefix. Save the script in a file namely, Change the permission using the command such as “chmod u+x”. Execute the script using the command such as ./ And, that is it. it would help you delete all the images starting with a specific prefix.

function removeUnwantedImages() {
  DOCKER_IMAGE_IDS=$(sudo docker images | grep "$imagePrefix" | awk '{print $3}') 
  if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then
    echo "---- No images available for deletion ----"
    sudo docker rmi -f $DOCKER_IMAGE_IDS

read -p "Delete images whose prefix is: " imagePrefix
Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking
Posted in Dockers. Tagged with .

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.