github.com/portworx/docker@v1.12.1/api/client/volume/remove.go (about)

     1  package volume
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/client"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
    14  	return &cobra.Command{
    15  		Use:     "rm VOLUME [VOLUME...]",
    16  		Aliases: []string{"remove"},
    17  		Short:   "Remove one or more volumes",
    18  		Long:    removeDescription,
    19  		Example: removeExample,
    20  		Args:    cli.RequiresMinArgs(1),
    21  		RunE: func(cmd *cobra.Command, args []string) error {
    22  			return runRemove(dockerCli, args)
    23  		},
    24  	}
    25  }
    26  
    27  func runRemove(dockerCli *client.DockerCli, volumes []string) error {
    28  	client := dockerCli.Client()
    29  	ctx := context.Background()
    30  	status := 0
    31  
    32  	for _, name := range volumes {
    33  		if err := client.VolumeRemove(ctx, name); err != nil {
    34  			fmt.Fprintf(dockerCli.Err(), "%s\n", err)
    35  			status = 1
    36  			continue
    37  		}
    38  		fmt.Fprintf(dockerCli.Out(), "%s\n", name)
    39  	}
    40  
    41  	if status != 0 {
    42  		return cli.StatusError{StatusCode: status}
    43  	}
    44  	return nil
    45  }
    46  
    47  var removeDescription = `
    48  Remove one or more volumes. You cannot remove a volume that is in use by a container.
    49  `
    50  
    51  var removeExample = `
    52  $ docker volume rm hello
    53  hello
    54  `