lima-vm/lima · error

failed to remove a directory %#q: %w

Error message

failed to remove a directory %#q: %w

What it means

After the disk image creation (CreateDisk) failed, Lima attempted to clean up by removing the freshly created disk directory, and that cleanup with os.RemoveAll also failed. Both errors are joined and then wrapped, so the message reports the directory whose removal failed alongside the original creation error.

Source

Thrown at cmd/limactl/disk.go:122

	if _, err := os.Stat(diskDir); !errors.Is(err, fs.ErrNotExist) {
		return fmt.Errorf("disk %#q already exists (%#q)", name, diskDir)
	}

	logrus.Infof("Creating %s disk %#q with size %s", format, name, units.BytesSize(float64(diskSize)))

	if err := os.MkdirAll(diskDir, 0o700); err != nil {
		return err
	}

	// qemu may not be available, use it only if needed.
	dataDisk := filepath.Join(diskDir, filenames.DataDisk)
	diskUtil := proxyimgutil.NewDiskUtil(ctx)
	err = diskUtil.CreateDisk(ctx, dataDisk, diskSize)
	if err != nil {
		rerr := os.RemoveAll(diskDir)
		if rerr != nil {
			err = errors.Join(err, fmt.Errorf("failed to remove a directory %#q: %w", diskDir, rerr))
		}
		return fmt.Errorf("failed to create %s disk in %#q: %w", format, diskDir, err)
	}

	return nil
}

func newDiskListCommand() *cobra.Command {
	diskListCommand := &cobra.Command{
		Use: "list",
		Example: `
To list existing disks:
$ limactl disk list
`,
		Short:   "List existing Lima disks",
		Aliases: []string{"ls"},
		Args:    WrapArgsError(cobra.ArbitraryArgs),
		RunE:    diskListAction,

View on GitHub (pinned to dd909d0973)

Solutions

  1. Read the joined creation error first (usually fix qemu-img availability or size)
  2. Fix permissions on the disk directory: chown -R $(whoami) ~/.lima/disks/<name>
  3. Remove leftover files/locks inside diskDir then delete it manually
  4. Re-run `limactl disk create` after cleanup

Example fix

// before
$ limactl disk create data --size 10GiB
// failed to create qcow2 disk in "...": ... failed to remove a directory "...": permission denied
// after
$ sudo chown -R $(whoami) ~/.lima/disks/data && rm -rf ~/.lima/disks/data
$ limactl disk create data --size 10GiB
Defensive patterns

Strategy: try-catch

Validate before calling

const dir = diskDirPath(name)
await fs.promises.access(path.dirname(dir), fs.constants.W_OK) // ensure parent writable for cleanup

Type guard

function isCleanupErr(err: unknown): boolean {
  return err instanceof Error && /failed to remove a directory/.test(err.message)
}

Try / catch

try {
  await createDisk(name, { size })
} catch (err) {
  if (isCleanupErr(err)) {
    console.error('Manual cleanup needed at the disk dir; fix perms and rm -rf')
  } else throw err
}

Prevention

When it happens

Trigger: `limactl disk create` where diskUtil.CreateDisk fails (e.g. qemu-img missing or the size invalid) AND os.RemoveAll(diskDir) fails due to permissions, EBUSY/EBUSY-like locks, or files added inside diskDir concurrently.

Common situations: qemu-img not installed or not on PATH combined with a home directory on a network mount holding locks; root-owned diskDir from a prior sudo run.

Related errors


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