Fix ctr delete to remove snapshot
Fix the behavior of removing snapshot on container delete.
Adds a flag to keep the snapshot if desired.
@@ -11,6 +11,12 @@ var deleteCommand = cli.Command{
Name: "delete",
Usage: "delete an existing container",
ArgsUsage: "CONTAINER",
+ Flags: []cli.Flag{
+ cli.BoolFlag{
+ Name: "keep-snapshot",
+ Usage: "do not clean up snapshot with container",
+ },
+ },
Action: func(context *cli.Context) error {
ctx, cancel := appContext(context)
defer cancel()
@@ -18,13 +24,17 @@ var deleteCommand = cli.Command{
if err != nil {
return err
}
+ deleteOpts := []containerd.DeleteOpts{}
+ if !context.Bool("keep-snapshot") {
+ deleteOpts = append(deleteOpts, containerd.WithRootFSDeletion)
+ }
container, err := client.LoadContainer(ctx, context.Args().First())
if err != nil {
return err
}
task, err := container.Task(ctx, nil)
if err != nil {
- return container.Delete(ctx)
+ return container.Delete(ctx, deleteOpts...)
}
status, err := task.Status(ctx)
if err != nil {
@@ -34,7 +44,7 @@ var deleteCommand = cli.Command{
if _, err := task.Delete(ctx); err != nil {
return err
}
- return container.Delete(ctx)
+ return container.Delete(ctx, deleteOpts...)
}
return fmt.Errorf("cannot delete a container with an existing task")
},
