├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── lib └── empty-recycle-bin.exe ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: macos-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Empty the [trash](https://en.wikipedia.org/wiki/Trash_(computing)). 3 | 4 | @example 5 | ``` 6 | import emptyTrash from 'empty-trash'; 7 | 8 | await emptyTrash(); 9 | ``` 10 | */ 11 | export default function emptyTrash(): Promise; 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {promisify} from 'node:util'; 3 | import childProcess from 'node:child_process'; 4 | import fs from 'node:fs/promises'; 5 | import path from 'node:path'; 6 | import {fileURLToPath} from 'node:url'; 7 | import {runJxa} from 'run-jxa'; 8 | import xdgTrashdir from 'xdg-trashdir'; 9 | import {pathExists} from 'path-exists'; 10 | import pFilter from 'p-filter'; 11 | 12 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); 13 | const execFileP = promisify(childProcess.execFile); 14 | 15 | const windowBinaryPath = path.join(__dirname, 'lib/empty-recycle-bin.exe'); 16 | 17 | const linuxEmptyTrash = async directory => { 18 | const files = await fs.readdir(directory); 19 | await Promise.all(files.map(file => fs.rm(path.join(directory, file), {recursive: true}))); 20 | }; 21 | 22 | const linuxEmptyTrashes = async () => { 23 | const directories = await pFilter(await xdgTrashdir.all(), pathExists); 24 | await Promise.all(directories.map(directory => linuxEmptyTrash(directory))); 25 | }; 26 | 27 | export default async function emptyTrash() { 28 | if (process.platform === 'darwin') { 29 | await runJxa(` 30 | const finder = Application('Finder'); 31 | 32 | if (finder.trash.items.length > 0) { 33 | finder.empty(); 34 | } 35 | `); 36 | return; 37 | } 38 | 39 | if (process.platform === 'win32') { 40 | await execFileP(windowBinaryPath); 41 | return; 42 | } 43 | 44 | await linuxEmptyTrashes(); 45 | } 46 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import emptyTrash from './index.js'; 3 | 4 | expectType>(emptyTrash()); 5 | -------------------------------------------------------------------------------- /lib/empty-recycle-bin.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/empty-trash/772aacce065245a1f1cd4b2565acfc2b932065b6/lib/empty-recycle-bin.exe -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "empty-trash", 3 | "version": "4.0.0", 4 | "description": "Empty the trash", 5 | "license": "MIT", 6 | "repository": "sindresorhus/empty-trash", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts", 24 | "lib" 25 | ], 26 | "keywords": [ 27 | "trash", 28 | "recycle", 29 | "file", 30 | "files", 31 | "directory", 32 | "directories", 33 | "empty", 34 | "clear", 35 | "macos", 36 | "windows", 37 | "linux" 38 | ], 39 | "dependencies": { 40 | "p-filter": "^3.0.0", 41 | "path-exists": "^5.0.0", 42 | "run-jxa": "^3.0.0", 43 | "xdg-trashdir": "^3.1.0" 44 | }, 45 | "devDependencies": { 46 | "ava": "^3.15.0", 47 | "trash": "^8.0.0", 48 | "tsd": "^0.18.0", 49 | "xo": "^0.45.0" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # empty-trash 2 | 3 | > Empty the [trash](https://en.wikipedia.org/wiki/Trash_(computing)) 4 | 5 | Works on macOS, Linux, and Windows. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install empty-trash 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import emptyTrash from 'empty-trash'; 17 | 18 | await emptyTrash(); 19 | ``` 20 | 21 | ## Info 22 | 23 | On macOS, AppleScript is used as it's the only way to do it without incurring permission issues. 24 | 25 | On Linux, the [XDG spec](https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html) is followed. 26 | 27 | On Windows, [`empty-recycle-bin`](https://github.com/sindresorhus/empty-recycle-bin) is used. 28 | 29 | ## Related 30 | 31 | - [empty-trash-cli](https://github.com/sindresorhus/empty-trash-cli) - CLI for this module 32 | - [trash](https://github.com/sindresorhus/trash) - Move files and folders to the trash 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import os from 'node:os'; 3 | import path from 'node:path'; 4 | import test from 'ava'; 5 | import trash from 'trash'; 6 | import emptyTrash from './index.js'; 7 | 8 | test('main', async t => { 9 | const file = 'empty-trash-fixture'; 10 | const trashFile = path.join(os.homedir(), '.Trash', file); 11 | fs.writeFileSync(file, ''); 12 | await trash([file]); 13 | t.true(fs.existsSync(trashFile)); 14 | await emptyTrash(); 15 | t.false(fs.existsSync(trashFile)); 16 | }); 17 | --------------------------------------------------------------------------------