6 "github.com/docker/cli/cli"
7 "github.com/docker/cli/cli/command"
8 "github.com/docker/cli/opts"
9 "github.com/spf13/cobra"
10 "golang.org/x/net/context"
13 type pruneOptions struct {
18 // NewPruneCommand returns a new cobra prune command for networks
19 func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
20 options := pruneOptions{filter: opts.NewFilterOpt()}
22 cmd := &cobra.Command{
23 Use: "prune [OPTIONS]",
24 Short: "Remove all unused networks",
26 RunE: func(cmd *cobra.Command, args []string) error {
27 output, err := runPrune(dockerCli, options)
32 fmt.Fprintln(dockerCli.Out(), output)
36 Tags: map[string]string{"version": "1.25"},
40 flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
41 flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
46 const warning = `WARNING! This will remove all networks not used by at least one container.
47 Are you sure you want to continue?`
49 func runPrune(dockerCli command.Cli, options pruneOptions) (output string, err error) {
50 pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
52 if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
56 report, err := dockerCli.Client().NetworksPrune(context.Background(), pruneFilters)
61 if len(report.NetworksDeleted) > 0 {
62 output = "Deleted Networks:\n"
63 for _, id := range report.NetworksDeleted {
71 // RunPrune calls the Network Prune API
72 // This returns the amount of space reclaimed and a detailed output string
73 func RunPrune(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
74 output, err := runPrune(dockerCli, pruneOptions{force: true, filter: filter})