The documentation for fs.rmdir is very short and doesn't explain the behavior of rmdir when the directory is not empty.
Q: What happens if I try to use this API to delete a non empty directory ?
Short answer: node.js fs.rmdir()
calls the POSIX rmdir()
; this will remove an empty directory, or return an error. In the given case, the call will invoke the callback function and pass the error as an exception.
The problem here is that the node.js documentation refers to POSIX:
The Node.js API Docs File System intro says:
File I/O is provided by simple wrappers around standard POSIX functions.
This almost changes the question into a duplicate of: Is there a listing of the POSIX API / functions?
The description for fs.rmdir
is terse, but sufficient.
Asynchronous rmdir(2).
The rmdir(2)
here is an implicit reference to the documentation for the rmdir() system call
. The number (2) here is an old unix man page convention to indicate Section 2 of the Manual pages, containing the kernel interfaces.
Although using a third-party library for such a thing I could not come up with a more elegant solution. So I ended up using the npm-module rimraf.
Install it
npm install rimraf
Or install it and save to 'package.json' (other save options can be found in the npm-install docs)
npm install --save rimraf
Then you can do the following:
rmdir = require('rimraf');
rmdir('some/directory/with/files', function(error){});
Or in Coffeescript:
rmdir = require 'rimraf'
rmdir 'some/directory/with/files', (error)->