github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/builder/prune.go (about) 1 package builder 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "strings" 8 9 "github.com/docker/cli/cli" 10 "github.com/docker/cli/cli/command" 11 "github.com/docker/cli/cli/command/completion" 12 "github.com/docker/cli/opts" 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/errdefs" 15 units "github.com/docker/go-units" 16 "github.com/spf13/cobra" 17 ) 18 19 type pruneOptions struct { 20 force bool 21 all bool 22 filter opts.FilterOpt 23 keepStorage opts.MemBytes 24 } 25 26 // NewPruneCommand returns a new cobra prune command for images 27 func NewPruneCommand(dockerCli command.Cli) *cobra.Command { 28 options := pruneOptions{filter: opts.NewFilterOpt()} 29 30 cmd := &cobra.Command{ 31 Use: "prune", 32 Short: "Remove build cache", 33 Args: cli.NoArgs, 34 RunE: func(cmd *cobra.Command, args []string) error { 35 spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options) 36 if err != nil { 37 return err 38 } 39 if output != "" { 40 fmt.Fprintln(dockerCli.Out(), output) 41 } 42 fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed))) 43 return nil 44 }, 45 Annotations: map[string]string{"version": "1.39"}, 46 ValidArgsFunction: completion.NoComplete, 47 } 48 49 flags := cmd.Flags() 50 flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation") 51 flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused build cache, not just dangling ones") 52 flags.Var(&options.filter, "filter", `Provide filter values (e.g. "until=24h")`) 53 flags.Var(&options.keepStorage, "keep-storage", "Amount of disk space to keep for cache") 54 55 return cmd 56 } 57 58 const ( 59 normalWarning = `WARNING! This will remove all dangling build cache. Are you sure you want to continue?` 60 allCacheWarning = `WARNING! This will remove all build cache. Are you sure you want to continue?` 61 ) 62 63 func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) { 64 pruneFilters := options.filter.Value() 65 pruneFilters = command.PruneFilters(dockerCli, pruneFilters) 66 67 warning := normalWarning 68 if options.all { 69 warning = allCacheWarning 70 } 71 if !options.force { 72 r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning) 73 if err != nil { 74 return 0, "", err 75 } 76 if !r { 77 return 0, "", errdefs.Cancelled(errors.New("builder prune has been cancelled")) 78 } 79 } 80 81 report, err := dockerCli.Client().BuildCachePrune(ctx, types.BuildCachePruneOptions{ 82 All: options.all, 83 KeepStorage: options.keepStorage.Value(), 84 Filters: pruneFilters, 85 }) 86 if err != nil { 87 return 0, "", err 88 } 89 90 if len(report.CachesDeleted) > 0 { 91 var sb strings.Builder 92 sb.WriteString("Deleted build cache objects:\n") 93 for _, id := range report.CachesDeleted { 94 sb.WriteString(id) 95 sb.WriteByte('\n') 96 } 97 output = sb.String() 98 } 99 100 return report.SpaceReclaimed, output, nil 101 } 102 103 // CachePrune executes a prune command for build cache 104 func CachePrune(ctx context.Context, dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error) { 105 return runPrune(ctx, dockerCli, pruneOptions{force: true, all: all, filter: filter}) 106 }