6 "github.com/docker/cli/cli"
7 "github.com/docker/cli/cli/command"
8 "github.com/docker/cli/opts"
9 units "github.com/docker/go-units"
10 "github.com/spf13/cobra"
11 "golang.org/x/net/context"
14 type pruneOptions struct {
19 // NewPruneCommand returns a new cobra prune command for volumes
20 func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
21 options := pruneOptions{filter: opts.NewFilterOpt()}
23 cmd := &cobra.Command{
24 Use: "prune [OPTIONS]",
25 Short: "Remove all unused volumes",
27 RunE: func(cmd *cobra.Command, args []string) error {
28 spaceReclaimed, output, err := runPrune(dockerCli, options)
33 fmt.Fprintln(dockerCli.Out(), output)
35 fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
38 Tags: map[string]string{"version": "1.25"},
42 flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
43 flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'label=<label>')")
48 const warning = `WARNING! This will remove all volumes not used by at least one container.
49 Are you sure you want to continue?`
51 func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
52 pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
54 if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
58 report, err := dockerCli.Client().VolumesPrune(context.Background(), pruneFilters)
63 if len(report.VolumesDeleted) > 0 {
64 output = "Deleted Volumes:\n"
65 for _, id := range report.VolumesDeleted {
68 spaceReclaimed = report.SpaceReclaimed
74 // RunPrune calls the Volume Prune API
75 // This returns the amount of space reclaimed and a detailed output string
76 func RunPrune(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
77 return runPrune(dockerCli, pruneOptions{force: true, filter: filter})