github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/pod_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/pkg/adapter" 8 "github.com/pkg/errors" 9 "github.com/sirupsen/logrus" 10 "github.com/spf13/cobra" 11 ) 12 13 var ( 14 podRmCommand cliconfig.PodRmValues 15 podRmDescription = fmt.Sprintf(`podman rm will remove one or more stopped pods and their containers from the host. 16 17 The pod name or ID can be used. A pod with containers will not be removed without --force. If --force is specified, all containers will be stopped, then removed.`) 18 _podRmCommand = &cobra.Command{ 19 Use: "rm [flags] POD [POD...]", 20 Short: "Remove one or more pods", 21 Long: podRmDescription, 22 RunE: func(cmd *cobra.Command, args []string) error { 23 podRmCommand.InputArgs = args 24 podRmCommand.GlobalFlags = MainGlobalOpts 25 podRmCommand.Remote = remoteclient 26 return podRmCmd(&podRmCommand) 27 }, 28 Args: func(cmd *cobra.Command, args []string) error { 29 return checkAllLatestAndCIDFile(cmd, args, false, false) 30 }, 31 Example: `podman pod rm mywebserverpod 32 podman pod rm -f 860a4b23 33 podman pod rm -f -a`, 34 } 35 ) 36 37 func init() { 38 podRmCommand.Command = _podRmCommand 39 podRmCommand.SetHelpTemplate(HelpTemplate()) 40 podRmCommand.SetUsageTemplate(UsageTemplate()) 41 flags := podRmCommand.Flags() 42 flags.BoolVarP(&podRmCommand.All, "all", "a", false, "Remove all running pods") 43 flags.BoolVarP(&podRmCommand.Force, "force", "f", false, "Force removal of a running pod by first stopping all containers, then removing all containers in the pod. The default is false") 44 flags.BoolVarP(&podRmCommand.Ignore, "ignore", "i", false, "Ignore errors when a specified pod is missing") 45 flags.BoolVarP(&podRmCommand.Latest, "latest", "l", false, "Remove the latest pod podman is aware of") 46 markFlagHiddenForRemoteClient("ignore", flags) 47 markFlagHiddenForRemoteClient("latest", flags) 48 } 49 50 // podRmCmd deletes pods 51 func podRmCmd(c *cliconfig.PodRmValues) error { 52 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 53 if err != nil { 54 return errors.Wrapf(err, "could not get runtime") 55 } 56 defer runtime.DeferredShutdown(false) 57 58 podRmIds, podRmErrors := runtime.RemovePods(getContext(), c) 59 for _, p := range podRmIds { 60 fmt.Println(p) 61 } 62 if len(podRmErrors) == 0 { 63 return nil 64 } 65 // Grab the last error 66 lastError := podRmErrors[len(podRmErrors)-1] 67 // Remove the last error from the error slice 68 podRmErrors = podRmErrors[:len(podRmErrors)-1] 69 70 for _, err := range podRmErrors { 71 logrus.Errorf("%q", err) 72 } 73 return lastError 74 }