DEV Community

loading...
Cover image for What is rimraf and how to use it in Node.js

What is rimraf and how to use it in Node.js

coderslang profile image Coderslang: Become a Software Engineer Originally published at learn.coderslang.com on ・1 min read

The rimraf command is an alternative to the Linux command rm -rf. It allows you to do deep recursive deletion of files and folders.

Global rimraf installation

You can install rimraf globally using npm. It’s a common module, so you can install it on any operating system that supports npm. Windows, Linux, macOS - you shouldn’t have any issues here.

> npm install rimraf --global
Enter fullscreen mode Exit fullscreen mode

Now you can use the command rimraf from the command line.

> rimraf ./node_modules
Enter fullscreen mode Exit fullscreen mode

Such a call will delete the directory node_modules and all its content.

Using rimraf in the Node.js project

Also, you can save rimraf in your current Node.js project and use it in JavaScript code.

> npm install rimraf --save
Enter fullscreen mode Exit fullscreen mode

This becomes handy when you need to delete some data that became obsolete.

import rimraf from 'rimraf';

// ...
// ...
// ...

.finally(() => {
        rimraf(`./${userIdFolder}/`, () => console.log(`DELETED ./${userIdFolder}/`));
      });
Enter fullscreen mode Exit fullscreen mode

Such a call can be attached to the Promise chain and will delete users' data when the processing is over and we don’t need it anymore.

Learn Full Stack JavaScript

Discussion (0)

Forem Open with the Forem app