How to use the rmdirSync function from fs-extra

Find comprehensive JavaScript fs-extra.rmdirSync code examples handpicked from public code repositorys.

fs-extra.rmdirSync is a method in the fs-extra library that synchronously removes a directory and all its contents in a file system.

202
203
204
205
206
207
208
209
210
211
212
    track();


    // Delete the dist folder
    if (fsExtra.existsSync(_distFolder)) {
      log(chalk.yellow(`Deleting dist folder: ${_distFolder}`));
      fsExtra.rmdirSync(_distFolder, { recursive: true });
    }
    fsExtra.mkdirSync(_distFolder);
  }
}
fork icon114
star icon127
watch icon0

3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
        } else {
            // rm fiilename
            fs.unlinkSync(filename);
        }
    }
    fs.rmdirSync(dir);
};


DataPacksJob.prototype.downloadVplDatapack = async function (jobInfo) {
    var deletedOnce = false;
fork icon80
star icon94
watch icon26

+ 17 other calls in file

How does fs-extra.rmdirSync work?

Sure! fs-extra.rmdirSync is a method in the fs-extra library that synchronously removes a directory and all its contents in a file system. When you call fs-extra.rmdirSync, you pass in the path to the directory you want to remove. The method then recursively removes all files and subdirectories within the directory, before removing the directory itself. fs-extra.rmdirSync operates synchronously, which means that it blocks the execution of other code until the directory removal process is complete. This makes it useful for small directories and when you want to ensure that the removal process is complete before continuing with the rest of the code. If the directory to be removed does not exist, fs-extra.rmdirSync will throw an error. If any of the files or subdirectories within the directory cannot be removed (for example, because they are in use by another process), an error will be thrown. In addition to fs-extra.rmdirSync, the fs-extra library provides a wide range of other methods for working with files and directories in a file system, such as fs-extra.readdirSync, fs-extra.copySync, and fs-extra.ensureDirSync.

296
297
298
299
300
301
302
303
304
305
exports.rmEmptyDirsSync = function (dirname) {
  let self = this
  var paths = fs.readdirSync(dirname)
  // console.log(dirname,paths);
  if (!paths.length) {
    fs.rmdirSync(dirname)
    return true
  } else {
    var count = 0
    paths.forEach(function (p) {
fork icon0
star icon2
watch icon0

292
293
294
295
296
297
298
299
300
301
let i = 0; // retry counter
do {
    try {
        if (!opts.excludes.length || !opts.excludes.some(item => curPath.indexOf(item) >= 0)) {
            emptyDirectory(curPath);
            fs.rmdirSync(curPath);
        }
        removed = true;
    }
    catch (e) {
fork icon0
star icon0
watch icon1

+ 6 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const fs = require("fs-extra");

const dirPath = "/path/to/directory";

try {
  fs.rmdirSync(dirPath, { recursive: true });
  console.log(`Directory '${dirPath}' and all its contents have been removed.`);
} catch (err) {
  console.error(err);
}

In this example, we first import the fs-extra library using the require function. We then define a variable dirPath containing the path to the directory we want to remove. We use a try...catch block to handle any errors that might occur during the directory removal process. We call the fs.rmdirSync() method, passing in the dirPath variable and an options object with the recursive property set to true. This tells the method to recursively remove all files and subdirectories within the directory, before removing the directory itself. If the directory and all its contents are successfully removed, we log a success message to the console. If an error occurs, we log the error to the console using console.error(). This code demonstrates how fs-extra.rmdirSync can be used to remove a directory and all its contents in a file system.

246
247
248
249
250
251
252
253
254
255
const result = spawnSync('npx', ['@twitter/create-twitter-app', `${name}-ui`], { cwd: appDir, stdio: 'inherit' });
console.log();

if (result.status === 1) {
  console.log(chalk.red('There was a problem installing the UI app with create-twitter-app. Aborting.'));
  fs.rmdirSync(appDir);
  process.exit(1);
}

// Rename the result to UI
fork icon0
star icon0
watch icon1

+ 3 other calls in file

370
371
372
373
374
375
376
377
378
379
// check if directory is empty
let curDir = path.dirname(file);

while (curDir !== path.resolve(baseDir, stopper)) {
    if (fs.existsSync(curDir) && fs.readdirSync(curDir).length === 0) {
        fs.rmdirSync(curDir);
        curDir = path.resolve(curDir, '..');
    } else {
        // directory not empty...do nothing
        break;
fork icon0
star icon0
watch icon0

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)