Deleting all images associated with an install

Admission - haven’t got a clue what I am doing…

Have installed Docker a while back, and installed Nextcloud. Coming back to it now, and would like to remove all the Nextcloud stuff, and start again, but…
When I am trying to remove ( docker rmi -f $(docker images -a | grep “nextcloud” | awk ‘{print $3}’) ), I get eg:

Error response from daemon: invalid reference format: repository name (library/268MB) must be lowercase
Error response from daemon: invalid reference format: repository name (library/424MB) must be lowercase

due to the fact that the “disk sizes” are all shown uppercase - eg 268MB, and therefore it won’t work…

IMAGE                                               ID             DISK USAGE   CONTENT SIZE   EXTRA
ghcr.io/nextcloud-releases/aio-apache:latest        65e10ecd0db3        268MB           63MB        
ghcr.io/nextcloud-releases/aio-clamav:latest        8f3ec1b80054        424MB          163MB

How do I get around this??
Thanks.

Of couse, it is not going to work if you use the image size (=$3) instead of the image name (=$1) or id (=$2)

Either one of those should work:

By ID:

docker rmi -f $(docker images -a | grep “nextcloud” | awk ‘{print $2}’)

By NAME:

docker rmi -f $(docker images -a | grep “nextcloud” | awk ‘{print $1}’)

Can’t get either of those to work?

john@media:~$ docker rmi -f $(docker images -a | grep “nextcloud” | awk ‘{print $2}’)
awk: 1: unexpected character 0xe2
awk: line 2: missing } near end of file
docker: 'docker rmi' requires at least 1 argument

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

See 'docker rmi --help' for more information

And:

john@media:~$ docker rmi -f $(docker images -a | grep “nextcloud” | awk ‘{print $1}’)
awk: 1: unexpected character 0xe2
awk: line 2: missing } near end of file
docker: 'docker rmi' requires at least 1 argument

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Thanks.

I took your command and hightlighted that you used the wrong column in your awk statement.

I missed that your command used wrong characters for the single and double quotes. If you type the command in the terminal, it should work, as long as at least one image exists that has “nextcloud” in it.

Some additional notes.

When a chain of commands don’t work, check the output of each command to see if everything is as expected. Then you can reslize yourself you used the wrong number as @meyay already pointed out.
The error message is also descriptive if you know “library” is the default namespace (in other words: organization or owner) of the official Docker images on Docker Hub, which is used when the owner is not set. So when the error messages complains about repository name, the command handles the input as repository name. Since you had a docker rmi command, that reveals you passed the image size to the docker rmi command instead of the image name.

The second error message is trickier and happened because you didn’t use code blocks for the quoted first command, only for the error message, so the forum changed the quotation marks to characters used in printed documents, not in a code. So be careful with copy-pasting :slight_smile:

By the way, if you don’t want to accidentally delete an image called for example "yourregistry/nextcloud-helper`, which has nothing to do with the nextcloud-releases, you can also make the command more specific and simpler at the same time:

docker images -q 'ghcr.io/nextcloud-releases/*' | xargs -- docker rmi

The first part:

docker images -q 'ghcr.io/nextcloud-releases/*' 

Lists the short image ID of all images in the ghcr.io registry under the nextcloud-releases namespace. The second part after the pipe character takes all returned image IDs and executes docker rmi IMAGEID, where IMAGEID is one image ID from the list of image IDs returned by the first part.

You could be this specific with grep as well, but that would just make the command more complex not simpler with regular expressions.

update:

Or you could also use labels and delete the images created by a specific vendor if the image is labelled properly

docker images -q --filter "label=org.opencontainers.image.vendor=Nextcloud"

So deleting would be:

docker images -q --filter "label=org.opencontainers.image.vendor=Nextcloud" | xargs -- docker rmi

But it only works as long as you don’t have multiple tags for the same image ID, so you might want to use one of these commands for listing the images:

docker images --filter "label=org.opencontainers.image.vendor=Nextcloud" --format '{{ .Repository }}:{{ .Tag }}'

or

docker images 'ghcr.io/nextcloud-releases/*' --format '{{ .Repository }}:{{ .Tag }}'

Then you would pass image tags to the docker rmi command and it would not complain about trying to delete an image by ID when multiple image tags refer to it.

Thanks for the detailed responses, @rimelek and @meyay - appreciated, and hopefully I have learned something!
Does seem to be a little complex to me, as a newby.
Will continue playing!

Thanks.