Skip to content

Commit 37fd612

Browse files
committed
Don't prune volumes on docker system prune
Volumes tend to carry important data and pruning them on `docker system prune` can easily cause unwanted data loss. Let's play it safe and not prune volumes on `system prune` by default, and instead provide an option. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 883d28c commit 37fd612

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

cli/command/system/prune.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
)
1313

1414
type pruneOptions struct {
15-
force bool
16-
all bool
17-
filter opts.FilterOpt
15+
force bool
16+
all bool
17+
pruneVolumes bool
18+
filter opts.FilterOpt
1819
}
1920

2021
// NewPruneCommand creates a new cobra.Command for `docker prune`
@@ -34,6 +35,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
3435
flags := cmd.Flags()
3536
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
3637
flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused images not just dangling ones")
38+
flags.BoolVar(&options.pruneVolumes, "volumes", false, "Prune volumes")
3739
flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'label=<key>=<value>')")
3840
// "filter" flag is available in 1.28 (docker 17.04) and up
3941
flags.SetAnnotation("filter", "version", []string{"1.28"})
@@ -67,12 +69,15 @@ func runPrune(dockerCli command.Cli, options pruneOptions) error {
6769
}
6870

6971
var spaceReclaimed uint64
70-
71-
for _, pruneFn := range []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
72+
pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
7273
prune.RunContainerPrune,
73-
prune.RunVolumePrune,
7474
prune.RunNetworkPrune,
75-
} {
75+
}
76+
if options.pruneVolumes {
77+
pruneFuncs = append(pruneFuncs, prune.RunVolumePrune)
78+
}
79+
80+
for _, pruneFn := range pruneFuncs {
7681
spc, output, err := pruneFn(dockerCli, options.filter)
7782
if err != nil {
7883
return err

0 commit comments

Comments
 (0)