github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/pods_prune.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/pkg/adapter"
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var (
    11  	podPruneCommand     cliconfig.PodPruneValues
    12  	podPruneDescription = `
    13  	podman pod prune
    14  
    15  	Removes all exited pods
    16  `
    17  	_prunePodsCommand = &cobra.Command{
    18  		Use:   "prune",
    19  		Args:  noSubArgs,
    20  		Short: "Remove all stopped pods and their containers",
    21  		Long:  podPruneDescription,
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			podPruneCommand.InputArgs = args
    24  			podPruneCommand.GlobalFlags = MainGlobalOpts
    25  			return podPruneCmd(&podPruneCommand)
    26  		},
    27  	}
    28  )
    29  
    30  func init() {
    31  	podPruneCommand.Command = _prunePodsCommand
    32  	podPruneCommand.SetHelpTemplate(HelpTemplate())
    33  	podPruneCommand.SetUsageTemplate(UsageTemplate())
    34  	flags := podPruneCommand.Flags()
    35  	flags.BoolVarP(&podPruneCommand.Force, "force", "f", false, "Force removal of all running pods.  The default is false")
    36  }
    37  
    38  func podPruneCmd(c *cliconfig.PodPruneValues) error {
    39  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    40  	if err != nil {
    41  		return errors.Wrapf(err, "could not get runtime")
    42  	}
    43  	defer runtime.DeferredShutdown(false)
    44  
    45  	ok, failures, err := runtime.PrunePods(getContext(), c)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	return printCmdResults(ok, failures)
    50  }