How to pass --force and --all flags when using rest api for pruning?

Hello I am using Go docker client to develop an app.

The problem is I can’t find any REST request that equivalent to command docker image prune -af

the function ImagesPrune works like command docker system prune and unfortunately I can’t pass any flags to it.

I also attempted to send request to API with curl like this:

curl --unix-socket /var/run/docker.sock -X POST "http://localhost/images/prune"

which returned {"ImagesDeleted":null,"SpaceReclaimed":0} as response so it works like command docker image prune as well.

I tried query parameters but no avail also I can’t find force or all flags on docker api docs as well.

So what request the command docker image prune -af or docker system prune -af sends?

Check the API reference

https://docs.docker.com/engine/api/v1.46/#tag/Image/operation/ImagePrune

  • dangling=<boolean> When set to true (or 1), prune only unused and untagged images. When set to false (or 0), all unused images are pruned.

The above quote is from the “filters” section.

Then check the SDK reference

https://pkg.go.dev/github.com/docker/docker/client#Client.ImagesPrune

It shows you can pass filters.

-f should not be necessary as there is nothing to ask for confirmation so you don’t need to disable it.

2 Likes

Thanks for your help rimelek!

I used parameter at first but my mistake was setting the dangling to true. I set the dangling to false and it worked as intended.

pruneFilters := filters.NewArgs()
pruneFilters.Add("dangling", "false")

rep, err := cli.ImagesPrune(ctx, pruneFilters)