image_delete.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package containerd
  2. import (
  3. "context"
  4. "github.com/containerd/containerd/images"
  5. "github.com/docker/distribution/reference"
  6. "github.com/docker/docker/api/types"
  7. )
  8. // ImageDelete deletes the image referenced by the given imageRef from this
  9. // daemon. The given imageRef can be an image ID, ID prefix, or a repository
  10. // reference (with an optional tag or digest, defaulting to the tag name
  11. // "latest"). There is differing behavior depending on whether the given
  12. // imageRef is a repository reference or not.
  13. //
  14. // If the given imageRef is a repository reference then that repository
  15. // reference will be removed. However, if there exists any containers which
  16. // were created using the same image reference then the repository reference
  17. // cannot be removed unless either there are other repository references to the
  18. // same image or force is true. Following removal of the repository reference,
  19. // the referenced image itself will attempt to be deleted as described below
  20. // but quietly, meaning any image delete conflicts will cause the image to not
  21. // be deleted and the conflict will not be reported.
  22. //
  23. // There may be conflicts preventing deletion of an image and these conflicts
  24. // are divided into two categories grouped by their severity:
  25. //
  26. // Hard Conflict:
  27. // - a pull or build using the image.
  28. // - any descendant image.
  29. // - any running container using the image.
  30. //
  31. // Soft Conflict:
  32. // - any stopped container using the image.
  33. // - any repository tag or digest references to the image.
  34. //
  35. // The image cannot be removed if there are any hard conflicts and can be
  36. // removed if there are soft conflicts only if force is true.
  37. //
  38. // If prune is true, ancestor images will each attempt to be deleted quietly,
  39. // meaning any delete conflicts will cause the image to not be deleted and the
  40. // conflict will not be reported.
  41. //
  42. // TODO(thaJeztah): implement ImageDelete "force" options; see https://github.com/moby/moby/issues/43850
  43. // TODO(thaJeztah): implement ImageDelete "prune" options; see https://github.com/moby/moby/issues/43849
  44. // TODO(thaJeztah): add support for image delete using image (short)ID; see https://github.com/moby/moby/issues/43854
  45. // TODO(thaJeztah): mage delete should send image "untag" events and prometheus counters; see https://github.com/moby/moby/issues/43855
  46. func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error) {
  47. parsedRef, err := reference.ParseNormalizedNamed(imageRef)
  48. if err != nil {
  49. return nil, err
  50. }
  51. ref := reference.TagNameOnly(parsedRef)
  52. err = i.client.ImageService().Delete(ctx, ref.String(), images.SynchronousDelete())
  53. if err != nil {
  54. return nil, err
  55. }
  56. return []types.ImageDeleteResponseItem{{Untagged: reference.FamiliarString(parsedRef)}}, nil
  57. }