# How to delete a non-empty directory in Node.js

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Apr 12, 2020, updated Aug 27, 2022. Topics: Node.js.
Canonical URL: https://attacomsian.com/blog/nodejs-delete-directory

> A quick article to learn how to delete a directory that is not empty by using Node.js.

In a Node.js application, you can use the `fs.rmdir()` method to delete a directory. This method works asynchronously to remove the directory. If the directory is not empty, you can pass an optional `recursive` flag to delete all nested files and folders recursively.

Here is an example:

```javascript
const fs = require('fs')

// directory path
const dir = 'temp'

// delete directory recursively
fs.rmdir(dir, { recursive: true }, err => {
  if (err) {
    throw err
  }

  console.log(`${dir} is deleted!`)
})
```

The `fs` module also contains a synchronous variant of the `rmdir()` method called `rmdirSync()` that removes the given folder synchronously. It blocks the Node.js event loop execution until the delete operation is not completed.

Let us look at the below example that deletes a folder synchronously:

```javascript
const fs = require('fs')

// directory path
const dir = 'temp'

// delete directory recursively
try {
  fs.rmdirSync(dir, { recursive: true })

  console.log(`${dir} is deleted!`)
} catch (err) {
  console.error(`Error while deleting ${dir}.`)
}
```

In Node.js `v16.x`, the `recursive` option is depreciated for the `fs.rmdir()` method. Instead, use the `fs.rm()` method to delete non-empty folders:

```javascript
const fs = require('fs')

// directory path
const dir = 'temp'

// delete directory recursively
fs.rm(dir, { recursive: true, force: true }, err => {
  if (err) {
    throw err
  }

  console.log(`${dir} is deleted!`)
})
```

If you using an older version of Node.js, just use the [del](https://www.npmjs.com/package/del) module from the NPM registry.

The `del` module is, by far, the easiest, safest, and most cross-platform option available to remove non-empty directories in Node.js. It provides a modern JavaScript API and supports both [promises](https://attacomsian.com/blog/promises-javascript) and [async-await](https://attacomsian.com/blog/async-await-javascript) syntax.

Install the `del` module by running the following command in your terminal:

```bash
$ npm install del --save
```

Here is an example that demonstrates how to use the `del` module to delete a directory recursively:

```javascript
const del = require('del')

// directory path
const dir = 'temp'

// delete directory recursively
;(async () => {
  try {
    await del(dir)

    console.log(`${dir} is deleted!`)
  } catch (err) {
    console.error(`Error while deleting ${dir}.`)
  }
})()
```

To move the directory to the trash instead of deleting it permanently, you can use the [trash](https://github.com/sindresorhus/trash) package which only moves files and folders to the trash.

Take a look at [this guide](https://attacomsian.com/blog/reading-writing-files-nodejs) to learn more about reading and writing files in a Node.js application.
