lima-vm/lima · error

failed to remove %#q: %w

Error message

failed to remove %#q: %w

What it means

pkg/instance.Delete failed at the final step: os.RemoveAll(inst.Dir) could not delete the instance directory under ${LIMA_HOME}. The driver resources were already unregistered, so only filesystem cleanup failed. The message quotes the directory path and wraps the OS error.

Source

Thrown at pkg/instance/delete.go:33

)

func Delete(ctx context.Context, inst *limatype.Instance, force bool) error {
	if inst.Protected {
		return errors.New("instance is protected to prohibit accidental removal (Hint: use `limactl unprotect`)")
	}
	if !force && inst.Status != limatype.StatusStopped {
		return fmt.Errorf("expected status %#q, got %#q", limatype.StatusStopped, inst.Status)
	}

	StopForcibly(inst)

	if len(inst.Errors) == 0 {
		if err := unregister(ctx, inst); err != nil {
			return fmt.Errorf("failed to unregister %#q: %w", inst.Dir, err)
		}
	}
	if err := os.RemoveAll(inst.Dir); err != nil {
		return fmt.Errorf("failed to remove %#q: %w", inst.Dir, err)
	}

	return nil
}

func unregister(ctx context.Context, inst *limatype.Instance) error {
	limaDriver, err := driverutil.CreateConfiguredDriver(ctx, inst, 0)
	if err != nil {
		return fmt.Errorf("failed to create driver instance: %w", err)
	}

	if err := limaDriver.Delete(ctx); err != nil {
		return fmt.Errorf("failed to delete driver instance: %w", err)
	}
	server.Stop(inst.Dir, true)

	return nil
}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Read the wrapped OS error to find the offending path; check ownership/permissions of ~/.lima/<instance>.
  2. Stop lingering processes (hostagent/guestagent) and retry.
  3. Delete manually with elevated permissions: `sudo rm -rf ~/.lima/<instance>` (only after confirming no VM is running).
  4. Move LIMA_HOME off synced/locked filesystems for future instances.

Example fix

// before (permission denied)
rm -rf ~/.lima/myvm
// after
sudo rm -rf ~/.lima/myvm
Defensive patterns

Strategy: validation

Validate before calling

dir := inst.Dir
if info, err := os.Stat(dir); err == nil {
    if err := unix.Access(dir, unix.W_OK); err != nil {
        return fmt.Errorf("cannot remove %s: not writable", dir)
    }
}

Try / catch

if err := instance.Delete(ctx, inst, false); err != nil {
    if strings.Contains(err.Error(), "failed to remove") {
        // resources unregistered; safe to clean up manually with elevated perms
        return os.RemoveAll(inst.Dir)
    }
    return err
}

Prevention

When it happens

Trigger: instance.Delete reaches os.RemoveAll(inst.Dir) after successful (or skipped) unregister, and RemoveAll fails — permission denied on files owned by another user/root, immutable files, or files still held open on Windows.

Common situations: LIMA_HOME inside a root-owned or synced/locked folder (Dropbox/OneDrive), running limactl as a different user than the one who created the VM, or a lingering process holding files open.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/90bb25b4329d5169. Report an issue: GitHub.