github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/volume/prune.go (about) 1 package volume 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/docker/cli/cli" 8 "github.com/docker/cli/cli/command" 9 "github.com/docker/cli/cli/command/completion" 10 "github.com/docker/cli/opts" 11 "github.com/docker/docker/api/types/versions" 12 "github.com/docker/docker/errdefs" 13 units "github.com/docker/go-units" 14 "github.com/pkg/errors" 15 "github.com/spf13/cobra" 16 ) 17 18 type pruneOptions struct { 19 all bool 20 force bool 21 filter opts.FilterOpt 22 } 23 24 // NewPruneCommand returns a new cobra prune command for volumes 25 func NewPruneCommand(dockerCli command.Cli) *cobra.Command { 26 options := pruneOptions{filter: opts.NewFilterOpt()} 27 28 cmd := &cobra.Command{ 29 Use: "prune [OPTIONS]", 30 Short: "Remove unused local volumes", 31 Args: cli.NoArgs, 32 RunE: func(cmd *cobra.Command, args []string) error { 33 spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options) 34 if err != nil { 35 return err 36 } 37 if output != "" { 38 fmt.Fprintln(dockerCli.Out(), output) 39 } 40 fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed))) 41 return nil 42 }, 43 Annotations: map[string]string{"version": "1.25"}, 44 ValidArgsFunction: completion.NoComplete, 45 } 46 47 flags := cmd.Flags() 48 flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused volumes, not just anonymous ones") 49 flags.SetAnnotation("all", "version", []string{"1.42"}) 50 flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation") 51 flags.Var(&options.filter, "filter", `Provide filter values (e.g. "label=<label>")`) 52 53 return cmd 54 } 55 56 const ( 57 unusedVolumesWarning = `WARNING! This will remove anonymous local volumes not used by at least one container. 58 Are you sure you want to continue?` 59 allVolumesWarning = `WARNING! This will remove all local volumes not used by at least one container. 60 Are you sure you want to continue?` 61 ) 62 63 func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) { 64 pruneFilters := command.PruneFilters(dockerCli, options.filter.Value()) 65 66 warning := unusedVolumesWarning 67 if versions.GreaterThanOrEqualTo(dockerCli.CurrentVersion(), "1.42") { 68 if options.all { 69 if pruneFilters.Contains("all") { 70 return 0, "", errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --all and --filter all=1")) 71 } 72 pruneFilters.Add("all", "true") 73 warning = allVolumesWarning 74 } 75 } else { 76 // API < v1.42 removes all volumes (anonymous and named) by default. 77 warning = allVolumesWarning 78 } 79 if !options.force { 80 r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning) 81 if err != nil { 82 return 0, "", err 83 } 84 if !r { 85 return 0, "", errdefs.Cancelled(errors.New("volume prune has been cancelled")) 86 } 87 } 88 89 report, err := dockerCli.Client().VolumesPrune(ctx, pruneFilters) 90 if err != nil { 91 return 0, "", err 92 } 93 94 if len(report.VolumesDeleted) > 0 { 95 output = "Deleted Volumes:\n" 96 for _, id := range report.VolumesDeleted { 97 output += id + "\n" 98 } 99 spaceReclaimed = report.SpaceReclaimed 100 } 101 102 return spaceReclaimed, output, nil 103 } 104 105 // RunPrune calls the Volume Prune API 106 // This returns the amount of space reclaimed and a detailed output string 107 func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) { 108 return runPrune(ctx, dockerCli, pruneOptions{force: true, filter: filter}) 109 }