You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.1 KiB

4 years ago
  1. # del [![Build Status](https://travis-ci.org/sindresorhus/del.svg?branch=master)](https://travis-ci.org/sindresorhus/del) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
  2. > Delete files and folders using [globs](https://github.com/isaacs/minimatch#usage)
  3. 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.
  4. ---
  5. <p align="center">🐶</p>
  6. <p align="center"><b>Support this project and improve your JavaScript skills with this great <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</b><br>Try his free <a href="https://javascript30.com/friend/AWESOME">JavaScript 30 course</a> for a taste of what to expect. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React</a> and <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> course.</p>
  7. ---
  8. ## Install
  9. ```
  10. $ npm install del
  11. ```
  12. ## Usage
  13. ```js
  14. const del = require('del');
  15. (async () => {
  16. const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']);
  17. console.log('Deleted files and folders:\n', deletedPaths.join('\n'));
  18. })();
  19. ```
  20. ## Beware
  21. The glob pattern `**` matches all children and *the parent*.
  22. So this won't work:
  23. ```js
  24. del.sync(['public/assets/**', '!public/assets/goat.png']);
  25. ```
  26. You have to explicitly ignore the parent directories too:
  27. ```js
  28. del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
  29. ```
  30. Suggestions on how to improve this welcome!
  31. ## API
  32. ### del(patterns, [options])
  33. Returns a promise for an array of deleted paths.
  34. ### del.sync(patterns, [options])
  35. Returns an array of deleted paths.
  36. #### patterns
  37. Type: `string` `string[]`
  38. See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage).
  39. - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js)
  40. - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
  41. #### options
  42. Type: `Object`
  43. See the [`glob` options](https://github.com/isaacs/node-glob#options).
  44. ##### force
  45. Type: `boolean`<br>
  46. Default: `false`
  47. Allow deleting the current working directory and outside.
  48. ##### dryRun
  49. Type: `boolean`<br>
  50. Default: `false`
  51. See what would be deleted.
  52. ```js
  53. const del = require('del');
  54. (async () => {
  55. const deletedPaths = await del(['tmp/*.js'], {dryRun: true});
  56. console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n'));
  57. })();
  58. ```
  59. ##### concurrency
  60. Type: `number`<br>
  61. Default: `Infinity`<br>
  62. Minimum: `1`
  63. Concurrency limit.
  64. ## CLI
  65. 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.
  66. ## Related
  67. - [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
  68. - [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching
  69. ## License
  70. MIT © [Sindre Sorhus](https://sindresorhus.com)