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