1package main
2
3import (
4	"os"
5	"path/filepath"
6
7	"github.com/codegangsta/cli"
8	"github.com/opencontainers/runc/libcontainer"
9)
10
11var deleteCommand = cli.Command{
12	Name:  "delete",
13	Usage: "delete any resources held by the container often used with detached containers",
14	ArgsUsage: `<container-id>
15
16Where "<container-id>" is the name for the instance of the container.
17
18For example, if the container id is "ubuntu01" and runc list currently shows the
19status of "ubuntu01" as "destroyed" the following will delete resources held for
20"ubuntu01" removing "ubuntu01" from the runc list of containers:
21
22       # runc delete ubuntu01`,
23	Action: func(context *cli.Context) {
24		container, err := getContainer(context)
25		if err != nil {
26			if lerr, ok := err.(libcontainer.Error); ok && lerr.Code() == libcontainer.ContainerNotExists {
27				// if there was an aborted start or something of the sort then the container's directory could exist but
28				// libcontainer does not see it because the state.json file inside that directory was never created.
29				path := filepath.Join(context.GlobalString("root"), context.Args().First())
30				if err := os.RemoveAll(path); err == nil {
31					return
32				}
33			}
34			fatal(err)
35		}
36		destroy(container)
37	},
38}
39