github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/volume/remove.go (about) 1 package volume 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 ) 13 14 type removeOptions struct { 15 force bool 16 17 volumes []string 18 } 19 20 func newRemoveCommand(dockerCli command.Cli) *cobra.Command { 21 var opts removeOptions 22 23 cmd := &cobra.Command{ 24 Use: "rm [OPTIONS] VOLUME [VOLUME...]", 25 Aliases: []string{"remove"}, 26 Short: "Remove one or more volumes", 27 Long: removeDescription, 28 Example: removeExample, 29 Args: cli.RequiresMinArgs(1), 30 RunE: func(cmd *cobra.Command, args []string) error { 31 opts.volumes = args 32 return runRemove(dockerCli, &opts) 33 }, 34 } 35 36 flags := cmd.Flags() 37 flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of one or more volumes") 38 flags.SetAnnotation("force", "version", []string{"1.25"}) 39 return cmd 40 } 41 42 func runRemove(dockerCli command.Cli, opts *removeOptions) error { 43 client := dockerCli.Client() 44 ctx := context.Background() 45 46 var errs []string 47 48 for _, name := range opts.volumes { 49 if err := client.VolumeRemove(ctx, name, opts.force); err != nil { 50 errs = append(errs, err.Error()) 51 continue 52 } 53 fmt.Fprintf(dockerCli.Out(), "%s\n", name) 54 } 55 56 if len(errs) > 0 { 57 return errors.Errorf("%s", strings.Join(errs, "\n")) 58 } 59 return nil 60 } 61 62 var removeDescription = ` 63 Remove one or more volumes. You cannot remove a volume that is in use by a container. 64 ` 65 66 var removeExample = ` 67 $ docker volume rm hello 68 hello 69 `