{"record":{"id":"6cea03dd6c7a0fc3","repo":"lima-vm/lima","slug":"failed-to-remove-a-directory-q-w","errorCode":null,"errorMessage":"failed to remove a directory %#q: %w","messagePattern":"failed to remove a directory %#q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/limactl/disk.go","lineNumber":122,"sourceCode":"\n\tif _, err := os.Stat(diskDir); !errors.Is(err, fs.ErrNotExist) {\n\t\treturn fmt.Errorf(\"disk %#q already exists (%#q)\", name, diskDir)\n\t}\n\n\tlogrus.Infof(\"Creating %s disk %#q with size %s\", format, name, units.BytesSize(float64(diskSize)))\n\n\tif err := os.MkdirAll(diskDir, 0o700); err != nil {\n\t\treturn err\n\t}\n\n\t// qemu may not be available, use it only if needed.\n\tdataDisk := filepath.Join(diskDir, filenames.DataDisk)\n\tdiskUtil := proxyimgutil.NewDiskUtil(ctx)\n\terr = diskUtil.CreateDisk(ctx, dataDisk, diskSize)\n\tif err != nil {\n\t\trerr := os.RemoveAll(diskDir)\n\t\tif rerr != nil {\n\t\t\terr = errors.Join(err, fmt.Errorf(\"failed to remove a directory %#q: %w\", diskDir, rerr))\n\t\t}\n\t\treturn fmt.Errorf(\"failed to create %s disk in %#q: %w\", format, diskDir, err)\n\t}\n\n\treturn nil\n}\n\nfunc newDiskListCommand() *cobra.Command {\n\tdiskListCommand := &cobra.Command{\n\t\tUse: \"list\",\n\t\tExample: `\nTo list existing disks:\n$ limactl disk list\n`,\n\t\tShort:   \"List existing Lima disks\",\n\t\tAliases: []string{\"ls\"},\n\t\tArgs:    WrapArgsError(cobra.ArbitraryArgs),\n\t\tRunE:    diskListAction,","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/cmd/limactl/disk.go#L104-L140","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Read the joined creation error first (usually fix qemu-img availability or size)","Fix permissions on the disk directory: chown -R $(whoami) ~/.lima/disks/<name>","Remove leftover files/locks inside diskDir then delete it manually","Re-run `limactl disk create` after cleanup"],"exampleFix":"// before\n$ limactl disk create data --size 10GiB\n// failed to create qcow2 disk in \"...\": ... failed to remove a directory \"...\": permission denied\n// after\n$ sudo chown -R $(whoami) ~/.lima/disks/data && rm -rf ~/.lima/disks/data\n$ limactl disk create data --size 10GiB","handlingStrategy":"try-catch","validationCode":"const dir = diskDirPath(name)\nawait fs.promises.access(path.dirname(dir), fs.constants.W_OK) // ensure parent writable for cleanup","typeGuard":"function isCleanupErr(err: unknown): boolean {\n  return err instanceof Error && /failed to remove a directory/.test(err.message)\n}","tryCatchPattern":"try {\n  await createDisk(name, { size })\n} catch (err) {\n  if (isCleanupErr(err)) {\n    console.error('Manual cleanup needed at the disk dir; fix perms and rm -rf')\n  } else throw err\n}","preventionTips":["Keep ~/.lima/disks on a local filesystem, not NFS","Ensure qemu-img is installed to avoid creation failure in the first place","Never run disk create under sudo (avoids root-owned dirs cleanup can't remove)"],"tags":["disk","cleanup","permissions"],"backgroundTag":"disk-cleanup-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}