Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/instance/delete.go
2611 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package instance
5
6
import (
7
"context"
8
"errors"
9
"fmt"
10
"os"
11
12
"github.com/lima-vm/lima/v2/pkg/driverutil"
13
"github.com/lima-vm/lima/v2/pkg/limatype"
14
)
15
16
func Delete(ctx context.Context, inst *limatype.Instance, force bool) error {
17
if inst.Protected {
18
return errors.New("instance is protected to prohibit accidental removal (Hint: use `limactl unprotect`)")
19
}
20
if !force && inst.Status != limatype.StatusStopped {
21
return fmt.Errorf("expected status %q, got %q", limatype.StatusStopped, inst.Status)
22
}
23
24
StopForcibly(inst)
25
26
if len(inst.Errors) == 0 {
27
if err := unregister(ctx, inst); err != nil {
28
return fmt.Errorf("failed to unregister %q: %w", inst.Dir, err)
29
}
30
}
31
if err := os.RemoveAll(inst.Dir); err != nil {
32
return fmt.Errorf("failed to remove %q: %w", inst.Dir, err)
33
}
34
35
return nil
36
}
37
38
func unregister(ctx context.Context, inst *limatype.Instance) error {
39
limaDriver, err := driverutil.CreateConfiguredDriver(inst, 0)
40
if err != nil {
41
return fmt.Errorf("failed to create driver instance: %w", err)
42
}
43
44
return limaDriver.Delete(ctx)
45
}
46
47