{"record":{"id":"2654993be4ac66d7","repo":"peass-ng/PEASS-ng","slug":"error-dir-not-empty-265499","errorCode":"ERROR_DIR_NOT_EMPTY","errorMessage":"({145}) The directory is not empty: [{pathLp}]","messagePattern":"\\((.+?)\\) The directory is not empty: \\[(.+?)\\]","errorType":"exception","errorClass":"DirectoryNotEmptyException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.DeleteDirectoryNative.cs","lineNumber":58,"sourceCode":"            // RemoveDirectory() / RemoveDirectoryTransacted()\n            // 2014-09-09: MSDN confirms LongPath usage.\n\n            // RemoveDirectory on a symbolic link will remove the link itself.\n\n            ? NativeMethods.RemoveDirectory(pathLp)\n\n            : NativeMethods.RemoveDirectoryTransacted(pathLp, transaction.SafeHandle);\n\n\n         var lastError = Marshal.GetLastWin32Error();\n\n         if (!success)\n         {\n            switch ((uint) lastError)\n            {\n               case Win32Errors.ERROR_DIR_NOT_EMPTY:\n                  // MSDN: .NET 3.5+: IOException: The directory specified by path is not an empty directory. \n                  throw new DirectoryNotEmptyException(pathLp, true);\n\n\n               case Win32Errors.ERROR_DIRECTORY:\n                  // MSDN: .NET 3.5+: DirectoryNotFoundException: Path refers to a file instead of a directory.\n                  if (File.ExistsCore(transaction, false, pathLp, PathFormat.LongFullPath))\n                     throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, \"({0}) {1}\", lastError, string.Format(CultureInfo.InvariantCulture, Resources.Target_Directory_Is_A_File, pathLp)));\n                  break;\n\n\n               case Win32Errors.ERROR_PATH_NOT_FOUND:\n                  if (continueOnNotFound)\n                     return;\n                  break;\n\n\n               case Win32Errors.ERROR_SHARING_VIOLATION:\n                  // MSDN: .NET 3.5+: IOException: The directory is being used by another process or there is an open handle on the directory.\n                  NativeError.ThrowException(lastError, pathLp);","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.DeleteDirectoryNative.cs#L40-L76","documentation":"DeleteDirectoryNative throws DirectoryNotEmptyException (Win32 error 145, ERROR_DIR_NOT_EMPTY) when the native RemoveDirectory call fails because the directory still contains entries. This surfaces from Directory.Delete when recursive=false (or recursion failed to empty it), since non-recursive deletion only works on empty directories. It wraps the OS error with the offending path in the message.","triggerScenarios":"Calling Directory.Delete(path) or Delete(path, recursive:false) on a directory containing files/subdirectories; DeleteDirectoryCore invoked from DeleteEmptySubdirectoriesCore or junction teardown on a folder that gained new entries between emptiness checks and deletion; hidden/system files present that made the folder appear empty.","commonSituations":"Deleting a temp/cache folder that an application is still writing to; race condition where another process created a file after the caller verified the directory was empty; passing recursive:false by default overload while assuming recursive behavior like Directory.Delete(path, true).","solutions":["Call Directory.Delete(path, true) with recursive:true to remove contents first.","Enumerate and delete contents explicitly, then delete the now-empty directory.","Retry after ensuring no other process is writing into the directory (close handles/stop services).","Catch DirectoryNotEmptyException and decide per-path whether to force-delete or skip."],"exampleFix":"// before\nDirectory.Delete(tempDir); // fails: not empty\n// after\nDirectory.Delete(tempDir, true); // recursive delete","handlingStrategy":"try-catch","validationCode":"if (Directory.EnumerateFileSystemEntries(path).Any())\n    Directory.Delete(path, true); // recursive for non-empty\nelse\n    Directory.Delete(path, false);","typeGuard":null,"tryCatchPattern":"try { Directory.Delete(path, false); }\ncatch (DirectoryNotEmptyException) { Directory.Delete(path, true); }","preventionTips":["Default to recursive:true unless you truly require empty-only deletes","Check for hidden/system files that make folders look empty","Account for concurrent writers when assuming a directory is empty","Use the recursive overload consistently in cleanup code"],"tags":["windows","filesystem","directory-not-empty","delete"],"backgroundTag":"directory-not-empty","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}