{"record":{"id":"ef32056648d681e4","repo":"apache/hadoop","slug":"can-not-delete-the-directory-s-as-it-is-not-e","errorCode":null,"errorMessage":"Can not delete the directory: [%s], as it is not empty and option recursive is false.","messagePattern":"Can not delete the directory: \\[(.+?)\\], as it is not empty and option recursive is false\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":318,"sourceCode":"      LOG.debug(\"Ready to delete the file: [{}], but it does not exist.\", f);\n      return false;\n    }\n    Path absolutePath = makeAbsolute(f);\n    String key = pathToKey(absolutePath);\n    if (key.compareToIgnoreCase(\"/\") == 0) {\n      FileStatus[] fileStatuses = listStatus(f);\n      return this.rejectRootDirectoryDelete(\n          fileStatuses.length == 0, recursive);\n    }\n\n    if (status.isDirectory()) {\n      if (!key.endsWith(PATH_DELIMITER)) {\n        key += PATH_DELIMITER;\n      }\n      if (!recursive && listStatus(f).length > 0) {\n        String errMsg = String.format(\"Can not delete the directory: [%s], as\"\n            + \" it is not empty and option recursive is false.\", f);\n        throw new IOException(errMsg);\n      }\n\n      createParent(f);\n\n      String priorLastKey = null;\n      do {\n        PartialListing listing = store.list(\n            key,\n            Constants.COS_MAX_LISTING_LENGTH,\n            priorLastKey,\n            true);\n        for (FileMetadata file : listing.getFiles()) {\n          store.delete(file.getKey());\n        }\n        for (FileMetadata commonPrefix : listing.getCommonPrefixes()) {\n          store.delete(commonPrefix.getKey());\n        }\n        priorLastKey = listing.getPriorLastKey();","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L300-L336","documentation":"delete(f, false) on a directory that contains at least one entry throws IOException stating the directory is not empty and recursive is false. CosN lists the directory via listStatus before deleting, and any file or common prefix blocks a non-recursive delete, matching POSIX rmdir semantics rather than the silent prefix-delete some object stores offer.","triggerScenarios":"fs.delete(dir, false) when listStatus(dir).length > 0; hadoop fs -rm cosn://bucket/dir (no -r) on a populated directory; 'hidden' children such as _SUCCESS, .crc files, or 0-byte directory markers making an apparently empty directory non-empty.","commonSituations":"Attempting rmdir where a previous job left _SUCCESS or part files; directory markers left by other COS tools; porting code from an object store whose delete on a prefix silently succeeded.","solutions":["Delete recursively: fs.delete(dir, true) or hadoop fs -rm -r.","If selective cleanup is needed, list children and delete them explicitly, then remove the directory.","If the directory should be empty, run fs.listStatus(dir) to find leftover entries (_SUCCESS, .crc, markers) and remove them before retrying."],"exampleFix":"// before\nfs.delete(dir, false); // IOException: ... not empty and option recursive is false\n\n// after\nboolean ok = fs.delete(dir, true);\n// or explicit:\nfor (FileStatus st : fs.listStatus(dir)) { fs.delete(st.getPath(), true); }\nok = fs.delete(dir, false);","handlingStrategy":"validation","validationCode":"if (fs.getFileStatus(dir).isDirectory() && !recursive) {\n  FileStatus[] children = fs.listStatus(dir);\n  if (children.length > 0) {\n    // decide: recursive delete, explicit cleanup, or skip\n    for (FileStatus st : children) { fs.delete(st.getPath(), true); }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.delete(dir, false);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains('not empty')) {\n    fs.delete(dir, true);\n  } else { throw e; }\n}","preventionTips":["Decide up front whether cleanup is recursive and pass the flag explicitly","Expect job protocol files (_SUCCESS, .crc, logs) to make directories non-empty","Use FileUtil.fullyDelete(fs, path) when POSIX-style behavior is wanted"],"tags":["cosn","hadoop","delete","directory-not-empty","rmdir"],"backgroundTag":"directory-not-empty","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}