emptyDir(dir, [callback])

Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.

Alias: emptydir()

  • dir <String>
  • callback <Function>

Example:

  1. const fs = require('fs-extra')
  2. // assume this directory has a lot of files and folders
  3. fs.emptyDir('/tmp/some/dir', err => {
  4. if (err) return console.error(err)
  5. console.log('success!')
  6. })
  7. // With promises
  8. fs.emptyDir('/tmp/some/dir')
  9. .then(() => {
  10. console.log('success!')
  11. })
  12. .catch(err => {
  13. console.error(err)
  14. })