Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs/command output for volume prune to specify when named volumes will be deleted #4079

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/command/system/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func confirmationMessage(dockerCli command.Cli, options pruneOptions) string {
"all networks not used by at least one container",
}
if options.pruneVolumes {
warnings = append(warnings, "all volumes not used by at least one container")
warnings = append(warnings, "all anonymous volumes not used by at least one container")
}
if options.all {
warnings = append(warnings, "all images without at least one container associated to them")
Expand Down
14 changes: 11 additions & 3 deletions cli/command/volume/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {

cmd := &cobra.Command{
Use: "prune [OPTIONS]",
Short: "Remove all unused local volumes",
Short: "Remove unused local volumes",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
spaceReclaimed, output, err := runPrune(dockerCli, options)
Expand All @@ -47,13 +47,21 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

const warning = `WARNING! This will remove all local volumes not used by at least one container.
const warning = `WARNING! This will remove all %s local volumes not used by at least one container.
Are you sure you want to continue?`

func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())

if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
var removed_volumes string
if pruneFilters.Contains("all") &&
(pruneFilters.ExactMatch("all", "true") || pruneFilters.ExactMatch("all", "1")) {
removed_volumes = "named and anonymous"
} else {
removed_volumes = "anonymous"
}

if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), fmt.Sprintf(warning, removed_volumes)) {
return 0, "", nil
}

Expand Down
17 changes: 17 additions & 0 deletions cli/command/volume/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ func TestVolumePruneForce(t *testing.T) {
}
}

func TestVolumePrunePromptAllNo(t *testing.T) {
// FIXME(vdemeester) make it work..
skip.If(t, runtime.GOOS == "windows", "TODO: fix test on windows")

for _, input := range []string{"1", "true"} {
cli := test.NewFakeCli(&fakeClient{
volumePruneFunc: simplePruneFunc,
})

cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader("n"))))
cmd := NewPruneCommand(cli)
cmd.Flags().Set("filter", fmt.Sprintf("all=%s", input))
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-all-no.golden")
}
}

func TestVolumePrunePromptYes(t *testing.T) {
// FIXME(vdemeester) make it work..
skip.If(t, runtime.GOOS == "windows", "TODO: fix test on windows")
Expand Down
2 changes: 2 additions & 0 deletions cli/command/volume/testdata/volume-prune-all-no.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WARNING! This will remove all named and anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] Total reclaimed space: 0B
2 changes: 1 addition & 1 deletion cli/command/volume/testdata/volume-prune-no.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
WARNING! This will remove all local volumes not used by at least one container.
WARNING! This will remove all anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] Total reclaimed space: 0B
2 changes: 1 addition & 1 deletion cli/command/volume/testdata/volume-prune-yes.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
WARNING! This will remove all local volumes not used by at least one container.
WARNING! This will remove all anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] Deleted Volumes:
foo
bar
Expand Down
18 changes: 16 additions & 2 deletions docs/reference/commandline/volume_prune.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ Remove all unused local volumes

## Description

Remove all unused local volumes. Unused local volumes are those which are not referenced by any containers
Remove all unused anonymous local volumes. Unused local volumes are those which
are not referenced by any containers. Anonymous volumes have random names and
are created by Docker during container or service creation.

Named volumes can be also removed using the filter `all=1`.

## Examples

```console
$ docker volume prune

WARNING! This will remove all local volumes not used by at least one container.
WARNING! This will remove all anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
07c7bdf3e34ab76d921894c2b834f073721fccfbbcba792aa7648e3a7a664c2e

Total reclaimed space: 36 B

$ docker volume prune --filter all=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably showcase the new -a flag here instead.


WARNING! This will remove all anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
07c7bdf3e34ab76d921894c2b834f073721fccfbbcba792aa7648e3a7a664c2e
Expand All @@ -39,6 +52,7 @@ than one filter, then pass multiple flags (e.g., `--filter "foo=bar" --filter "b
The currently supported filters are:

* label (`label=<key>`, `label=<key>=<value>`, `label!=<key>`, or `label!=<key>=<value>`) - only remove volumes with (or without, in case `label!=...` is used) the specified labels.
* all (`all=0`, `all=1`) - whether named volumes should also be removed.

The `label` filter accepts two formats. One is the `label=...` (`label=<key>` or `label=<key>=<value>`),
which removes volumes with the specified labels. The other
Expand Down