Recursively delete all empty folders in a directory and child directories.
Install with npm:
$ npm install --save @hyperse/delete-empty-folders
import { deleteEmpty, deleteEmptySync } from '@hyperse/delete-empty-folders';
Given the following directory structure, the highlighted directories would be deleted.
foo/
└─┬ a/
- ├── aa/
├── bb/
│ └─┬ bbb/
│ │ ├── one.txt
│ │ └── two.txt
- ├── cc/
- ├ b/
- └ c/
If no callback is passed, a promise is returned. Returns the array of deleted directories.
(async () => {
let deleted = await deleteEmpty('foo');
console.log(deleted); //=> ['foo/aa/', 'foo/a/cc/', 'foo/b/', 'foo/c/']
})();
// or
deleteEmpty('foo/')
.then((deleted) => console.log(deleted)) //=> ['foo/aa/', 'foo/a/cc/', 'foo/b/', 'foo/c/']
.catch(console.error);
Returns the array of deleted directories.
console.log(deleteEmptySync('foo/')); //=> ['foo/aa/', 'foo/a/cc/', 'foo/b/', 'foo/c/']