Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/del
Delete files and directories
https://github.com/sindresorhus/del
Last synced: 3 days ago
JSON representation
Delete files and directories
- Host: GitHub
- URL: https://github.com/sindresorhus/del
- Owner: sindresorhus
- License: mit
- Created: 2014-06-21T20:24:49.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-10-07T04:55:37.000Z (2 months ago)
- Last Synced: 2024-11-28T10:03:03.967Z (14 days ago)
- Language: JavaScript
- Homepage:
- Size: 82 KB
- Stars: 1,326
- Watchers: 20
- Forks: 66
- Open Issues: 18
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome - sindresorhus/del - Delete files and directories (JavaScript)
- awesome-development - del - Delete files/folders using globs. (Packages / Filesystem)
- awesome-nodejs-cn - del - 使用 globs 删除文件/文件夹 (包 / 文件系统)
- awesome-nodejs - del - Delete files and directories. ![](https://img.shields.io/github/stars/sindresorhus/del.svg?style=social&label=Star) (Repository / Filesystem)
- awesome-nodejs-cn - del - **star:1325** 使用全局变量删除文件/文件夹 (包 / 文件系统)
- awesome-github-star - del
- awesome-nodejs - del - Delete files/folders using globs. (Packages / Filesystem)
- awesome-nodejs - del - Delete files and directories - ★ 876 (Filesystem)
- awesome-node - del - Delete files/folders using globs. (Packages / Filesystem)
- awesome-nodejs-cn - del - 使用Glob删除文件/文件夹. (目录 / 文件系统)
- awesome-nodejs - del - 删除文件和目录,基于 `rimraf` 和 `glob ` (Uncategorized / Uncategorized)
- awesome-node-modules - del - Delete files/folders using globs. (Uncategorized / Uncategorized)
README
# del
> Delete files and directories using [globs](https://github.com/sindresorhus/globby#globbing-patterns)
Similar to [rimraf](https://github.com/isaacs/rimraf), but with a Promise API and support for multiple files and globbing. It also protects you against deleting the current working directory and above.
## Install
```sh
npm install del
```## Usage
```js
import {deleteAsync} from 'del';const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
```## Beware
The glob pattern `**` matches all children and *the parent*.
So this won't work:
```js
deleteSync(['public/assets/**', '!public/assets/goat.png']);
```You have to explicitly ignore the parent directories too:
```js
deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
```To delete all subdirectories inside `public/`, you can do:
```js
deleteSync(['public/*/']);
```Suggestions on how to improve this welcome!
## API
Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`.
### deleteAsync(patterns, options?)
Returns `Promise` with the deleted paths.
### deleteSync(patterns, options?)
Returns `string[]` with the deleted paths.
#### patterns
Type: `string | string[]`
See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)#### options
Type: `object`
You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the below options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
##### force
Type: `boolean`\
Default: `false`Allow deleting the current working directory and outside.
##### dryRun
Type: `boolean`\
Default: `false`See what would be deleted.
```js
import {deleteAsync} from 'del';const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
```##### dot
Type: `boolean`\
Default: `false`Allow patterns to match files/folders that start with a period (`.`).
This option is passed through to [`fast-glob`](https://github.com/mrmlnc/fast-glob#dot).
Note that an explicit dot in a portion of the pattern will always match dot files.
**Example**
```
directory/
├── .editorconfig
└── package.json
``````js
import {deleteSync} from 'del';deleteSync('*', {dot: false});
//=> ['package.json']
deleteSync('*', {dot: true});
//=> ['.editorconfig', 'package.json']
```##### concurrency
Type: `number`\
Default: `Infinity`\
Minimum: `1`Concurrency limit.
##### onProgress
Type: `(progress: ProgressData) => void`
Called after each file or directory is deleted.
```js
import {deleteAsync} from 'del';await deleteAsync(patterns, {
onProgress: progress => {
// …
}});
```###### ProgressData
```js
{
totalCount: number,
deletedCount: number,
percent: number,
path?: string
}
```- `percent` is a value between `0` and `1`
- `path` is the absolute path of the deleted file or directory. It will not be present if nothing was deleted.## CLI
See [del-cli](https://github.com/sindresorhus/del-cli) for a CLI for this module and [trash-cli](https://github.com/sindresorhus/trash-cli) for a safe version that is suitable for running by hand.
## Related
- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
- [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching