github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/pods/rm.go (about)

     1  package pods
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/parse"
     8  	"github.com/containers/libpod/cmd/podmanV2/registry"
     9  	"github.com/containers/libpod/cmd/podmanV2/utils"
    10  	"github.com/containers/libpod/pkg/domain/entities"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    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  	rmCommand = &cobra.Command{
    19  		Use:   "rm [flags] POD [POD...]",
    20  		Short: "Remove one or more pods",
    21  		Long:  podRmDescription,
    22  		RunE:  rm,
    23  		Args: func(cmd *cobra.Command, args []string) error {
    24  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    25  		},
    26  		Example: `podman pod rm mywebserverpod
    27    podman pod rm -f 860a4b23
    28    podman pod rm -f -a`,
    29  	}
    30  )
    31  
    32  var (
    33  	rmOptions = entities.PodRmOptions{}
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: rmCommand,
    40  		Parent:  podCmd,
    41  	})
    42  
    43  	flags := rmCommand.Flags()
    44  	flags.BoolVarP(&rmOptions.All, "all", "a", false, "Restart all running pods")
    45  	flags.BoolVarP(&rmOptions.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")
    46  	flags.BoolVarP(&rmOptions.Ignore, "ignore", "i", false, "Ignore errors when a specified pod is missing")
    47  	flags.BoolVarP(&rmOptions.Latest, "latest", "l", false, "Restart the latest pod podman is aware of")
    48  	if registry.IsRemote() {
    49  		_ = flags.MarkHidden("latest")
    50  		_ = flags.MarkHidden("ignore")
    51  	}
    52  }
    53  
    54  func rm(cmd *cobra.Command, args []string) error {
    55  	var (
    56  		errs utils.OutputErrors
    57  	)
    58  	responses, err := registry.ContainerEngine().PodRm(context.Background(), args, rmOptions)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	// in the cli, first we print out all the successful attempts
    63  	for _, r := range responses {
    64  		if r.Err == nil {
    65  			fmt.Println(r.Id)
    66  		} else {
    67  			errs = append(errs, r.Err)
    68  		}
    69  	}
    70  	return errs.PrintErrors()
    71  }