This recipe migrates from the deprecated fs.rmdir() method (with recursive option) to the modern fs.rm() method for removing directories.
Deprecation
Node.js deprecated fs.rmdir() and its variants when used with the recursive option. Use fs.rm() instead.
See DEP0147 for more details.
Usage
Run this codemod with:
Before/After
Callback API
Synchronous API
Promise API
What It Does
- Replaces
fs.rmdir() with fs.rm() when recursive: true is used
- Replaces
fs.rmdirSync() with fs.rmSync() when recursive: true is used
- Replaces
fs.promises.rmdir() with fs.promises.rm() when recursive: true is used
- Adds
force: true option for safer directory removal
- Works with all three APIs: callback, synchronous, and promise-based
Why force: true?
The force: true option ensures that errors are ignored if the path doesn’t exist, making the operation more robust and matching the behavior of the deprecated rmdir with recursive: true.
Complete Example
The fs.rm() method is more powerful and should be used with caution. Always verify the path before calling fs.rm() with recursive: true to avoid accidentally deleting important files.
For simple directory removal without contents, you can still use fs.rmdir() without the recursive option. The deprecation only applies when recursive: true is specified.