@@ -331,7 +331,7 @@ func (i *Image) Containers() ([]string, error) {
331331
332332// removeContainers removes all containers using the image.
333333func (i * Image ) removeContainers (options * RemoveImagesOptions ) error {
334- if ! options .Force && ! options .ExternalContainers {
334+ if ( ! options .Force && ! options .ExternalContainers ) || options . DryRun {
335335 // Nothing to do.
336336 return nil
337337 }
@@ -487,13 +487,15 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma
487487
488488 // Only try to untag if we know it's not an ID or digest.
489489 if ! byID && ! byDigest {
490- if err := i .Untag (referencedBy ); handleError (err ) != nil {
491- return processedIDs , err
490+ if ! options .DryRun {
491+ if err := i .Untag (referencedBy ); handleError (err ) != nil {
492+ return processedIDs , err
493+ }
494+ report .Untagged = append (report .Untagged , referencedBy )
495+
496+ // If there's still tags left, we cannot delete it.
497+ skipRemove = len (i .Names ()) > 0
492498 }
493- report .Untagged = append (report .Untagged , referencedBy )
494-
495- // If there's still tags left, we cannot delete it.
496- skipRemove = len (i .Names ()) > 0
497499 }
498500 }
499501
@@ -528,11 +530,13 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma
528530 parent = nil
529531 }
530532
531- if _ , err := i .runtime .store .DeleteImage (i .ID (), true ); handleError (err ) != nil {
532- if errors .Is (err , storage .ErrImageUsedByContainer ) {
533- err = fmt .Errorf ("%w: consider listing external containers and force-removing image" , err )
533+ if ! options .DryRun {
534+ if _ , err := i .runtime .store .DeleteImage (i .ID (), true ); handleError (err ) != nil {
535+ if errors .Is (err , storage .ErrImageUsedByContainer ) {
536+ err = fmt .Errorf ("%w: consider listing external containers and force-removing image" , err )
537+ }
538+ return processedIDs , err
534539 }
535- return processedIDs , err
536540 }
537541
538542 report .Untagged = append (report .Untagged , i .Names ()... )
0 commit comments