# safe-wipe

> > Safely wipe a folder.

Latest version **0.3.0** (published 2025-03-05) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install safe-wipe
pnpm add safe-wipe
yarn add safe-wipe
bun add safe-wipe
```

## Health

**Score 35/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2025-03-05 |
| First published | 2014-10-07 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 2 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | pascalduez, valeriangalliat |

## Links

- npm: https://www.npmjs.com/package/safe-wipe
- Repository: https://github.com/themeleon/safe-wipe
- Issues: https://github.com/themeleon/safe-wipe/issues
- npm.io page: https://npm.io/package/safe-wipe

## Dependencies (2)

- [extend](https://npm.io/package/extend.md) ^3.0.2
- [rimraf](https://npm.io/package/rimraf.md) 2.*

## Recent versions

- 0.3.0 (latest) — 2025-03-05
- 0.2.5 — 2019-09-11
- 0.2.4 — 2015-05-25
- 0.2.3 — 2014-10-09
- 0.2.2 — 2014-10-07
- 0.2.1 — 2014-10-07
- 0.2.0 — 2014-10-07
- 0.1.0 — 2014-10-07

## README

# safe-wipe [![npm version](http://img.shields.io/npm/v/safe-wipe.svg?style=flat-square)](https://www.npmjs.org/package/safe-wipe)

> Safely wipe a folder.

Overview
--------

This is the perfect library for when you want to wipe a folder with
user's confirmation.

* If the folder is empty (or contains only useless files like
  `.DS_Store` or `Thumbs.db` as configured with `config.ignore`), the
  folder will be removed without asking anything.
* If `config.force` is set to `true`, wipe anyway.
* If the session is not interactive, raise an exception.
* Prompt the user for confirmation, and raise an exception if the answer
  is negative.

**Note:** This library requires Node.js v8.0.0 or higher as it uses native JavaScript promises and `util.promisify`.

You can configure the following variables:

| Name                        | Description                                                                                                                                            | Default                     |
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| `stdin`, `stdout`, `stderr` | Streams to use for I/O.                                                                                                                                | `process.*` streams         |
| `ignore`                    | An array of files to ignore when checking if a directory is empty.                                                                                     | `.DS_Store` and `Thumbs.db` |
| `parent`                    | A (supposed) parent directory of the directory to wipe. If the parent is contained in the directory to wipe, the process will be aborted in all cases. |                             |
| `interactive`               | Whether the session is interactive.                                                                                                                    | `true`                      |
| `force`                     | Whether to force the wipe if the folder is not empty.                                                                                                  | `false`                     |
| `silent`                    | The error messages are not printed if this is set to `true`.                                                                                          | `false`                     |
| `messages`                  | An object of messages for user prompt and error display.                                                                                               |                             |

The messages are:

| Name        | Description                                                                         |
|-------------|-------------------------------------------------------------------------------------|
| `contained` | Error message when the folder to wipe is contained in the configured parent folder. |
| `confirm`   | Text to prompt the user to confirm the (not empty) directory wipe.                  |
| `abort`     | Error message when the user refuses to wipe the folder.                             |

The function is asynchronous and returns a promise. Nothing is passed to
the success function, but you'll get an `Error` instance in the error
function. It can have the following `code` property:

| Code        | Description                                                                                            |
|-------------|--------------------------------------------------------------------------------------------------------|
| `CONTAINED` | Refused to remove the directory since it's containing the supposed parent.                             |
| `ABORT`     | The user aborted the operation (or we're not in an interactive session and `config.force` is `false`). |

Examples
--------

### Simple usage

```js
var safeWipe = require('safe-wipe');

safeWipe('directory', {
  parent: __dirname,
  messages: {
    abort: 'Nope.',
  },
}).then(function () {
  console.log('Successfully removed!');
}, function (e) {
  console.error(e.message, e.code);
});
```

### Bind a config object

```js
var mySafeWipe = safeWipe({
  interactive: false,
});

mySafeWipe('some-directory').then(function () {
  // ...
});

mySafeWipe('another-directory', {
  force: true,
});
```

---
_Source: https://npm.io/package/safe-wipe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
