go io_error ai_generated true

remove /path/to/dir: directory not empty

ID: go/os-remove-directory-not-empty

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-08-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.14 active
1.15 active
1.16 active

Root Cause

Attempting to remove a directory that still contains files or subdirectories using os.Remove() or os.RemoveAll() incorrectly.

generic

中文

使用 os.Remove() 或 os.RemoveAll() 尝试删除仍然包含文件或子目录的目录。

Workarounds

  1. 95% success
    Use os.RemoveAll() for recursive deletion.
  2. 90% success
    List directory contents and remove files first, then the directory.

Dead Ends

Common approaches that don't work:

  1. 40% fail

    os.RemoveAll() should work, but if it fails, it's due to permission issues or race conditions.

  2. 80% fail

    The first call fails, and the second call will also fail.