github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/system/prune.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package system
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/containerd/containerd"
    24  	"github.com/containerd/nerdctl/v2/pkg/api/types"
    25  	"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
    26  	"github.com/containerd/nerdctl/v2/pkg/cmd/container"
    27  	"github.com/containerd/nerdctl/v2/pkg/cmd/image"
    28  	"github.com/containerd/nerdctl/v2/pkg/cmd/network"
    29  	"github.com/containerd/nerdctl/v2/pkg/cmd/volume"
    30  )
    31  
    32  // Prune will remove all unused containers, networks,
    33  // images (dangling only or both dangling and unreferenced), and optionally, volumes.
    34  func Prune(ctx context.Context, client *containerd.Client, options types.SystemPruneOptions) error {
    35  	if err := container.Prune(ctx, client, types.ContainerPruneOptions{
    36  		GOptions: options.GOptions,
    37  		Stdout:   options.Stdout,
    38  	}); err != nil {
    39  		return err
    40  	}
    41  	if err := network.Prune(ctx, client, types.NetworkPruneOptions{
    42  		GOptions:             options.GOptions,
    43  		NetworkDriversToKeep: options.NetworkDriversToKeep,
    44  		Stdout:               options.Stdout,
    45  	}); err != nil {
    46  		return err
    47  	}
    48  	if options.Volumes {
    49  		if err := volume.Prune(ctx, client, types.VolumePruneOptions{
    50  			GOptions: options.GOptions,
    51  			All:      false,
    52  			Force:    true,
    53  			Stdout:   options.Stdout,
    54  		}); err != nil {
    55  			return err
    56  		}
    57  	}
    58  	if err := image.Prune(ctx, client, types.ImagePruneOptions{
    59  		Stdout:   options.Stdout,
    60  		GOptions: options.GOptions,
    61  		All:      options.All,
    62  	}); err != nil {
    63  		return nil
    64  	}
    65  
    66  	if options.BuildKitHost != "" {
    67  		prunedObjects, err := builder.Prune(ctx, types.BuilderPruneOptions{
    68  			Stderr:       options.Stderr,
    69  			GOptions:     options.GOptions,
    70  			All:          options.All,
    71  			BuildKitHost: options.BuildKitHost,
    72  		})
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		if len(prunedObjects) > 0 {
    78  			fmt.Fprintln(options.Stdout, "Deleted build cache objects:")
    79  			for _, item := range prunedObjects {
    80  				fmt.Fprintln(options.Stdout, item.ID)
    81  			}
    82  		}
    83  	}
    84  
    85  	// TODO: print total reclaimed space
    86  
    87  	return nil
    88  }