{"record":{"id":"e3eff7834aca60e3","repo":"apache/hadoop","slug":"can-not-delete-root-path","errorCode":null,"errorMessage":"Can not delete root path","messagePattern":"Can not delete root path","errorType":"exception","errorClass":"PathIOException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":274,"sourceCode":"      LOG.debug(\"Creating a new file: [{}] in COS.\", f);\n    }\n\n    Path absolutePath = makeAbsolute(f);\n    String key = pathToKey(absolutePath);\n    return new FSDataOutputStream(\n        new CosNOutputStream(getConf(), store, key, blockSize,\n            this.boundedIOThreadPool), statistics);\n  }\n\n  private boolean rejectRootDirectoryDelete(boolean isEmptyDir,\n      boolean recursive) throws PathIOException {\n    if (isEmptyDir) {\n      return true;\n    }\n    if (recursive) {\n      return false;\n    } else {\n      throw new PathIOException(this.bucket, \"Can not delete root path\");\n    }\n  }\n\n  @Override\n  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,\n      EnumSet<CreateFlag> flags, int bufferSize, short replication,\n      long blockSize, Progressable progress) throws IOException {\n    Path parent = f.getParent();\n    if (null != parent) {\n      if (!getFileStatus(parent).isDirectory()) {\n        throw new FileAlreadyExistsException(\"Not a directory: \" + parent);\n      }\n    }\n\n    return create(f, permission, flags.contains(CreateFlag.OVERWRITE),\n        bufferSize, replication, blockSize, progress);\n  }\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L256-L292","documentation":"CosNFileSystem.delete refuses to delete the filesystem root (the bucket itself) unless it is empty. rejectRootDirectoryDelete returns harmlessly when the root is empty, but a non-empty root combined with recursive=false throws PathIOException('Can not delete root path'). The guard exists because one recursive call at the root would wipe every object in the bucket.","triggerScenarios":"fs.delete(new Path('/'), false) on a bucket containing any object; hadoop fs -rm cosn://bucket/ (without -r) on a non-empty bucket; cleanup scripts whose path variable resolves to the root when empty.","commonSituations":"A 'base path' variable built from string concatenation that ends up empty and normalizes to the root; operating on the mount root of a cosn mount table; using -rm where -rmdir or an explicit child path was intended.","solutions":["Target the specific subdirectories or files to delete instead of the root.","If wiping the bucket is intended, use fs.delete(root, true) (hadoop fs -rm -r) and understand it deletes every object.","Fix the calling script so the delete path can never be the root: validate !path.isRoot() before delete."],"exampleFix":"// before\nfs.delete(new Path('/'), false); // PathIOException: Can not delete root path\n\n// after\nPath target = (base == null || base.isEmpty()) ? new Path('/data') : new Path(base);\nif (!target.isRoot()) {\n  fs.delete(target, true);\n}","handlingStrategy":"validation","validationCode":"Path p = base == null ? new Path('/') : new Path(base);\nif (p.isRoot()) {\n  // refuse, or expand into explicit children\n  for (FileStatus st : fs.listStatus(p)) { fs.delete(st.getPath(), true); }\n} else {\n  fs.delete(p, recursive);\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.delete(p, false);\n} catch (PathIOException e) {\n  if (p.isRoot()) { /* policy decision: enumerate children instead */ } else { throw e; }\n}","preventionTips":["Validate paths are non-root before every delete","Never build delete paths by concatenating possibly-empty variables","Prefer -rmdir for empty directories and explicit paths in cleanup scripts"],"tags":["cosn","hadoop","delete","root-path","path-io-exception"],"backgroundTag":"root-delete-forbidden","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}