What is rimraf.sync() in Node.js?

Key takeaways:

  • rimraf.sync() is a method in Node.js used to synchronously remove a directory and all its content.

  • It works on both empty and non-empty directories, unlike fs.rmdir(), which only removes empty directories.

  • The method requires importing rimraf as it's not part of the built-in fs module.

  • rimraf.sync() executes synchronously, meaning the rest of the code is blocked until the directory is removed.

  • It's commonly used when you need to remove a directory and all its files or subdirectories in one operation.

  • If the operation succeeds, no errors are thrown; if it fails, an error is thrown.

  • To handle errors, it's recommended to use try-catch blocks for better error management.

The fs module is a built-in file system module in Node.js that consists of several methods that can be used to access and manipulate files on the operating system. rimraf.sync() is a fs method used to remove an existing directory. It is important to note that it can remove a non-empty directory alongwith its content.

'folder 4' , a non empty directory, is removed.
'folder 4' , a non empty directory, is removed.

Required import

Import the Node.js package fs to access files and import rimraf to use the method.

const fs = require('fs');
const rimraf = require('rimraf');

Syntax

The syntax for rimraf.sync() using the try-catch block:

const rimraf = require('rimraf');
try {
rimraf.sync('path/to/directoryName');
console.log('Directory removed successfully');
} catch (error) {
console.error('Error removing directory:', error);
}

Parameters

  • sync: Specifies that the method is executed synchronously.

  • path: The destination of the directory that is to be removed.

  • directoryNameThe name of the directory that is to be removed.

  •  err: A callback function that is called once the method is executed and returns one of the two cases:

    • error: The method failed to remove the specified directory.

    • null: The method successfully removed the specified directory.

Example code

This code removes the SyncDir directory inside the public directory along with its contents.

Removes the specified directory.

Output explanation

Let’s understand the output of the above example code.

widget

  • Contents of the public directory are printed. It contains the SyncDir directory.

  • The rimraf.sync() method is executed, and the specified SyncDir is removed. Its successful case statement is immediately printed because synchronous commands do not allow the code to proceed until its callback is received.

  • Contents of the public directory are printed again. This time it does not contain the SyncDir directory because it has been successfully removed.

Question

Is there any alternate method to remove the empty directory?

Show Answer

Conclusion

In conclusion, rimraf.sync() is a powerful and efficient method for removing directories and their contents in Node.js. It simplifies the process of deleting non-empty directories, which fs.rmdir() cannot do. Since it operates synchronously, it ensures that the directory removal is completed before moving on to the next part of the code. However, it’s essential to handle potential errors using try-catch blocks to ensure smooth execution, especially when dealing with critical file system operations.

Unlock your full potential with our comprehensive course. Dive deeper into the topic and gain hands-on experience through expert-led lessons.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Is rimraf a dev dependency?

Yes, rimraf is typically added as a dev dependency because it is used for cleaning up files or directories during the development process, particularly for build scripts and automation.


What is rimraf dist?

rimraf dist is a command used to delete the dist directory, often used in JavaScript projects to remove build artifacts or old distribution files before creating new ones.


How to add rimraf?

You can add rimraf to your project by running the command: npm install --save-dev rimraf


Does rimraf work on Windows?

Yes, rimraf works on Windows. It is a cross-platform tool designed to handle file deletions on Unix-like systems and Windows.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved