github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/rm.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/containers/libpod/cmd/podman/cliconfig" 7 "github.com/containers/libpod/libpod/define" 8 "github.com/containers/libpod/pkg/adapter" 9 "github.com/pkg/errors" 10 "github.com/sirupsen/logrus" 11 "github.com/spf13/cobra" 12 ) 13 14 var ( 15 rmCommand cliconfig.RmValues 16 rmDescription = fmt.Sprintf(`Removes one or more containers from the host. The container name or ID can be used. 17 18 Command does not remove images. Running or unusable containers will not be removed without the -f option.`) 19 _rmCommand = &cobra.Command{ 20 Use: "rm [flags] CONTAINER [CONTAINER...]", 21 Short: "Remove one or more containers", 22 Long: rmDescription, 23 RunE: func(cmd *cobra.Command, args []string) error { 24 rmCommand.InputArgs = args 25 rmCommand.GlobalFlags = MainGlobalOpts 26 rmCommand.Remote = remoteclient 27 return rmCmd(&rmCommand) 28 }, 29 Args: func(cmd *cobra.Command, args []string) error { 30 return checkAllLatestAndCIDFile(cmd, args, false, true) 31 }, 32 Example: `podman rm imageID 33 podman rm mywebserver myflaskserver 860a4b23 34 podman rm --force --all 35 podman rm -f c684f0d469f2`, 36 } 37 ) 38 39 func init() { 40 rmCommand.Command = _rmCommand 41 rmCommand.SetHelpTemplate(HelpTemplate()) 42 rmCommand.SetUsageTemplate(UsageTemplate()) 43 flags := rmCommand.Flags() 44 flags.BoolVarP(&rmCommand.All, "all", "a", false, "Remove all containers") 45 flags.BoolVarP(&rmCommand.Ignore, "ignore", "i", false, "Ignore errors when a specified container is missing") 46 flags.BoolVarP(&rmCommand.Force, "force", "f", false, "Force removal of a running or unusable container. The default is false") 47 flags.BoolVarP(&rmCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of") 48 flags.BoolVar(&rmCommand.Storage, "storage", false, "Remove container from storage library") 49 flags.BoolVarP(&rmCommand.Volumes, "volumes", "v", false, "Remove anonymous volumes associated with the container") 50 flags.StringArrayVarP(&rmCommand.CIDFiles, "cidfile", "", nil, "Read the container ID from the file") 51 markFlagHiddenForRemoteClient("ignore", flags) 52 markFlagHiddenForRemoteClient("cidfile", flags) 53 markFlagHiddenForRemoteClient("latest", flags) 54 markFlagHiddenForRemoteClient("storage", flags) 55 } 56 57 // rmCmd removes one or more containers 58 func rmCmd(c *cliconfig.RmValues) error { 59 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 60 if err != nil { 61 return errors.Wrapf(err, "could not get runtime") 62 } 63 defer runtime.DeferredShutdown(false) 64 65 // Storage conflicts with --all/--latest/--volumes/--cidfile/--ignore 66 if c.Storage { 67 if c.All || c.Ignore || c.Latest || c.Volumes || c.CIDFiles != nil { 68 return errors.Errorf("--storage conflicts with --volumes, --all, --latest, --ignore and --cidfile") 69 } 70 } 71 72 ok, failures, err := runtime.RemoveContainers(getContext(), c) 73 if err != nil { 74 if len(c.InputArgs) < 2 { 75 exitCode = setExitCode(err) 76 } 77 return err 78 } 79 80 if len(failures) > 0 { 81 for _, err := range failures { 82 if errors.Cause(err) == define.ErrWillDeadlock { 83 logrus.Errorf("Potential deadlock detected - please run 'podman system renumber' to resolve") 84 } 85 exitCode = setExitCode(err) 86 } 87 } 88 89 return printCmdResults(ok, failures) 90 }