| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | |
| 11 | "github.com/sirupsen/logrus" |
| 12 | "github.com/spf13/cobra" |
| 13 | |
| 14 | "github.com/lima-vm/lima/v2/pkg/autostart" |
| 15 | "github.com/lima-vm/lima/v2/pkg/instance" |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 18 | "github.com/lima-vm/lima/v2/pkg/networks/reconcile" |
| 19 | "github.com/lima-vm/lima/v2/pkg/store" |
| 20 | ) |
| 21 | |
| 22 | func newDeleteCommand() *cobra.Command { |
| 23 | deleteCommand := &cobra.Command{ |
| 24 | Use: "delete INSTANCE [INSTANCE, ...]", |
| 25 | Aliases: []string{"remove", "rm"}, |
| 26 | Short: "Delete an instance of Lima", |
| 27 | Args: WrapArgsError(cobra.MinimumNArgs(1)), |
| 28 | RunE: deleteAction, |
| 29 | ValidArgsFunction: deleteBashComplete, |
| 30 | GroupID: basicCommand, |
| 31 | } |
| 32 | deleteCommand.Flags().BoolP("force", "f", false, "Forcibly kill the processes") |
| 33 | return deleteCommand |
| 34 | } |
| 35 | |
| 36 | func deleteAction(cmd *cobra.Command, args []string) error { |
| 37 | ctx := cmd.Context() |
| 38 | force, err := cmd.Flags().GetBool("force") |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | var errs []error |
| 43 | for _, instName := range args { |
| 44 | inst, err := store.Inspect(ctx, instName) |
| 45 | if err != nil { |
| 46 | if errors.Is(err, os.ErrNotExist) { |
| 47 | if err := warnAboutMissingInstance(instName); err != nil { |
| 48 | errs = append(errs, err) |
| 49 | } |
| 50 | continue |
| 51 | } |
| 52 | return err |
| 53 | } |
| 54 | if err := instance.Delete(ctx, inst, force); err != nil { |
| 55 | return fmt.Errorf("failed to delete instance %#q: %w", instName, err) |
| 56 | } |
| 57 | if registered, err := autostart.IsRegistered(ctx, inst); err != nil && !errors.Is(err, autostart.ErrNotSupported) { |
| 58 | logrus.WithError(err).Warnf("Failed to check if the autostart entry for instance %#q is registered", instName) |
| 59 | } else if registered { |
| 60 | if err := autostart.UnregisterFromStartAtLogin(ctx, inst); err != nil { |
| 61 | logrus.WithError(err).Warnf("Failed to unregister the autostart entry for instance %#q", instName) |
| 62 | } else { |
| 63 | logrus.Infof("The autostart entry for instance %#q has been unregistered", instName) |
| 64 | } |
| 65 | } |
| 66 | logrus.Infof("Deleted %#q (%#q)", instName, inst.Dir) |
| 67 | } |
| 68 | if err := reconcile.Reconcile(ctx, ""); err != nil { |
| 69 | errs = append(errs, err) |
| 70 | } |
| 71 | return errors.Join(errs...) |
| 72 | } |
| 73 | |
| 74 | func warnAboutMissingInstance(instName string) error { |
| 75 | instDir, err := dirnames.InstanceDir(instName) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if _, statErr := os.Stat(instDir); errors.Is(statErr, os.ErrNotExist) { |
| 80 | logrus.Warnf("Ignoring non-existent instance %#q", instName) |
| 81 | return nil |
| 82 | } else if statErr != nil { |
| 83 | return statErr |
| 84 | } |
| 85 | return fmt.Errorf("instance %#q directory %#q exists but its %#q could not be read; "+ |
| 86 | "it was NOT deleted, in case it is currently being cloned into. "+ |
| 87 | "If no `limactl` operation is using it, remove it manually", |
| 88 | instName, instDir, filenames.LimaYAML) |
| 89 | } |
| 90 | |
| 91 | func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 92 | return bashCompleteInstanceNames(cmd) |
| 93 | } |