https://github.com/sindresorhus/trash
Move files and directories to the trash
https://github.com/sindresorhus/trash
delete nodejs npm-package recycle-bin trash
Last synced: 5 days ago
JSON representation
Move files and directories to the trash
- Host: GitHub
- URL: https://github.com/sindresorhus/trash
- Owner: sindresorhus
- License: mit
- Created: 2014-05-14T17:25:32.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2024-07-26T12:21:13.000Z (over 1 year ago)
- Last Synced: 2025-05-03T00:26:34.237Z (9 months ago)
- Topics: delete, nodejs, npm-package, recycle-bin, trash
- Language: JavaScript
- Homepage:
- Size: 673 KB
- Stars: 2,585
- Watchers: 25
- Forks: 80
- Open Issues: 13
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- starred - sindresorhus/trash - Move files and directories to the trash (JavaScript)
- awesome-nodejs-cn - trash - 比`rm`更安全的替代命令. (目录 / 命令行应用)
- awesome-terminals - trash - Move files and directories to the trash. (Tools / Node)
- awesome-nodejs - trash - Move files and directories to the trash - ★ 1634 (Command-line apps)
- fucking-awesome-nodejs - trash - Safer alternative to `rm`. (Packages / Command-line apps)
- awesome-nodejs-cn - trash - 比 ```rm``` 更安全的选择 (包 / 命令行程序)
- awesome-nodejs - trash - Safer alternative to `rm`. (Packages / Command-line apps)
- awesome-nodejs - trash - 将文件移动到回收站,`rm` 的安全替代方法 (Uncategorized / Uncategorized)
- awesome-mac - trash - Move files and directories to the trash. ![Open-Source Software][OSS Icon] (Developer Tools / Command Line Tools)
- awesome-nodejs-precise - trash - square)]() | A safer alternative to <code>rm</code>. | (Packages / Command-line apps)
- awesome - trash
- awesome-nodejs-cn - trash - trash 命令替代linux rm命令实现windows回收站的功能 (包 / 命令行应用)
- awesome-nodejs-new - trash - Safer alternative to `rm`. (Packages / Command-line apps)
- awesome-nodejs - trash - Safer alternative to `rm`. (Packages / Command-line apps)
- awesome-starred-test - sindresorhus/trash - Move files and directories to the trash (JavaScript)
- awesome-nodejs-cn - trash - **star:2584** 更安全的 rm 命令替代品 ![star > 2000][Awesome] (包 / 命令行程序)
README
# 
> Move files and folders to the trash
Works on macOS (10.12+), Linux, and Windows (8+).
**Note:** The Linux implementation is not very good and not maintained. Help welcome. If no one steps up to help maintain it, I will eventually remove Linux support.
In contrast to [`fs.unlink`](https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback), [`del`](https://github.com/sindresorhus/del), and [`rimraf`](https://github.com/isaacs/rimraf) which permanently delete files, this only moves them to the trash, which is much safer and reversible.
## Install
```sh
npm install trash
```
## Usage
```js
import trash from 'trash';
await trash(['*.png', '!rainbow.png']);
```
## API
### trash(input, options?)
Returns a `Promise`.
#### input
Type: `string | string[]`
Accepts paths and [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
Non-existent files and glob patterns that match nothing are silently ignored. If you need to know whether files were actually trashed, check for their existence beforehand.
#### options
Type: `object`
##### glob
Type: `boolean`\
Default: `true`
Enable globbing when matching file paths.
## CLI
To install the [`trash`](https://github.com/sindresorhus/trash-cli) command, run:
```sh
npm install --global trash-cli
```
## Info
On macOS, [`macos-trash`](https://github.com/sindresorhus/macos-trash) is used.\
On Linux, the [XDG spec](https://specifications.freedesktop.org/trash/1.0/) is followed.\
On Windows, [`recycle-bin`](https://github.com/sindresorhus/recycle-bin) is used.
On WSL (Windows Subsystem for Linux), files are moved to the Windows Recycle Bin.
## FAQ
### But I can do the same thing with `mv`
Not really. The `mv` command isn't cross-platform and moving to trash is not just about moving the file to a "trash" directory. On all OSes you'll run into file conflicts. The user won't easily be able to restore the file. It won't work on an external drive. The trash directory location varies between Windows versions. For Linux, there's a whole [spec](https://standards.freedesktop.org/trash-spec/trashspec-1.0.html) you need to follow. On macOS, you'll lose the [Put back](https://mac-fusion.com/trash-tip-how-to-put-files-back-to-their-original-location/) feature.
### Windows service account behavior
When running as the Windows SYSTEM account or other service accounts, files are moved to that account's recycle bin (e.g., `C:\$Recycle.Bin\S-1-5-18\` for SYSTEM). These files won't be visible in the normal user's recycle bin and may accumulate over time. For service contexts, consider using direct file deletion instead.
## Related
- [trash-cli](https://github.com/sindresorhus/trash-cli) - CLI for this module
- [empty-trash](https://github.com/sindresorhus/empty-trash) - Empty the trash
- [del](https://github.com/sindresorhus/del) - Delete files and folders