summaryrefslogtreecommitdiff
path: root/cmd/podman/images_prune.go
blob: 06879e02d89bbe80f682e17c4ebf7c396e055f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

import (
	"fmt"
	"github.com/containers/libpod/cmd/podman/libpodruntime"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var (
	pruneImagesDescription = `
	podman image prune

	Removes all unnamed images from local storage
`

	pruneImagesCommand = cli.Command{
		Name:         "prune",
		Usage:        "Remove unused images",
		Description:  pruneImagesDescription,
		Action:       pruneImagesCmd,
		OnUsageError: usageErrorHandler,
	}
)

func pruneImagesCmd(c *cli.Context) error {
	runtime, err := libpodruntime.GetRuntime(c)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	pruneImages, err := runtime.ImageRuntime().GetPruneImages()
	if err != nil {
		return err
	}

	for _, i := range pruneImages {
		if err := i.Remove(true); err != nil {
			return errors.Wrapf(err, "failed to remove %s", i.ID())
		}
		fmt.Println(i.ID())
	}
	return nil
}