How to delete all the folders in a loop?

I don't redeem how to delete folders to one directory, you can primeneniy fs.rmdir, but how do you delete all directories in the path, for example, there is a complete path to file \uploads\io\qw\q\we\qw\rt\3449c9e5e332f1dbb81505cd739fbf3f.jpg delete the file by using fs.unlink, everything is OK, and then remains \uploads\io\qw\q\we\qw\rt\ and I'm trying to remove the folders in a loop, but it doesn't work. Removed by razbila line spliit('/"), remove the last element using .pop(), then convert the array into a string using join('/') and try to delete via fs.rmdir(), all in a loop, but it doesn't work.

let path = 'uploads/io/io/ui/yu/rt/q/'.split('/');
lastEl;

 for (var i = path.length - 1; i > 0; i--) {

 path = path.join('/');
console.log(path);

 fs.rmdir(path, (err) => {
 console.log('Deleted the directory' + path);
});

 console.log('Deleted the directory' + path);

 path = path.split('/');
 lastEl = path.pop();
 // console.log(oldPhotoPath);

}

/* Removed directory uploads/io/io/ui/yu/rt/q
uploads/io/io/ui/yu/rt
Removed directory uploads/io/io/ui/yu/rt
uploads/io/io/ui/yu
Removed directory uploads/io/io/ui/yu
uploads/io/io/ui
Removed directory uploads/io/io/ui
uploads/io/io
Removed directory uploads/io/io
uploads/io
Removed directory uploads/io
Removed directory uploads
Removed directory uploads
Removed directory uploads
Removed directory uploads
Removed directory uploads
Removed directory uploads */
March 12th 20 at 08:04
0 answer

Find more questions by tags Node.js