fsPromises.rmdir(path[, options])


  • path <string> | <Buffer> | <URL>
  • options <Object>
    • maxRetries <integer> 如果遇到 EBUSYEMFILEENFILEENOTEMPTYEPERM 错误,Node.js 将在每次尝试时以 retryDelay 毫秒的线性退避等待时间重试该操作。 此选项表示重试次数。 如果 recursive 选项不为 true,则忽略此选项。 默认值: 0
    • recursive <boolean> 如果为 true,则执行递归目录删除。 在递归模式下,当 path 不存在时不报错,当操作失败时则重试。 默认值: false
    • retryDelay <integer> 重试之间等待的时间(以毫秒为单位)。 如果 recursive 选项不为 true,则忽略此选项。 默认值: 100
  • 返回: <Promise> 成功时将使用 undefined 履行。

删除由 path 标识的目录。

在文件(而不是目录)上使用 fsPromises.rmdir() 会导致 promise 被拒绝,在 Windows 上使用 ENOENT 错误,在 POSIX 上使用 ENOTDIR 错误。

recursive 设置为 true 会导致类似于 Unix 命令 rm -rf 的行为:对于不存在的路径不会引发错误,并且表示文件的路径将被删除。 不推荐使用 recursive 选项的宽松行为,以后会抛出 ENOTDIRENOENT

  • path <string> | <Buffer> | <URL>
  • options <Object>
    • maxRetries <integer> If an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error is encountered, Node.js retries the operation with a linear backoff wait of retryDelay milliseconds longer on each try. This option represents the number of retries. This option is ignored if the recursive option is not true. Default: 0.
    • recursive <boolean> If true, perform a recursive directory removal. In recursive mode, errors are not reported if path does not exist, and operations are retried on failure. Default: false.
    • retryDelay <integer> The amount of time in milliseconds to wait between retries. This option is ignored if the recursive option is not true. Default: 100.
  • Returns: <Promise> Fulfills with undefined upon success.

Removes the directory identified by path.

Using fsPromises.rmdir() on a file (not a directory) results in the promise being rejected with an ENOENT error on Windows and an ENOTDIR error on POSIX.

Setting recursive to true results in behavior similar to the Unix command rm -rf: an error will not be raised for paths that do not exist, and paths that represent files will be deleted. The permissive behavior of the recursive option is deprecated, ENOTDIR and ENOENT will be thrown in the future.