{"id":15642971,"url":"https://github.com/binarykitchen/find-remove","last_synced_at":"2025-04-10T05:08:03.955Z","repository":{"id":474468,"uuid":"8698612","full_name":"binarykitchen/find-remove","owner":"binarykitchen","description":"recursively finds files by filter options from a start directory onwards and deletes these. useful if you want to clean up a directory in your node.js app.","archived":false,"fork":false,"pushed_at":"2025-03-06T06:45:04.000Z","size":1269,"stargazers_count":62,"open_issues_count":13,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T05:07:59.648Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.org/package/find-remove","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binarykitchen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-03-11T07:36:05.000Z","updated_at":"2025-03-06T06:45:08.000Z","dependencies_parsed_at":"2023-01-13T10:22:10.907Z","dependency_job_id":null,"html_url":"https://github.com/binarykitchen/find-remove","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarykitchen%2Ffind-remove","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarykitchen%2Ffind-remove/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarykitchen%2Ffind-remove/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binarykitchen%2Ffind-remove/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binarykitchen","download_url":"https://codeload.github.com/binarykitchen/find-remove/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161273,"owners_count":21057555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T11:58:27.999Z","updated_at":"2025-04-10T05:08:03.928Z","avatar_url":"https://github.com/binarykitchen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# find-remove\n\nfinally in typescript (since v5)\n\nrecursively finds files by filter options from a start directory onwards and deletes only those which meet conditions you can define. useful if you want to clean up a directory in your node.js app.\n\nyou can filter by extensions, names, level in directory structure, file creation date and ignore by name, yeah!\n\n## installation\n\nto install find-remove, use [npm](http://github.com/isaacs/npm):\n\n    $ npm install -S find-remove\n\nthen in your node.js app, get reference to the function like that:\n\n```ts\nimport findRemoveSync from \"find-remove\";\n```\n\n## quick examples\n\n### 1. delete all _.bak or _.log files within the /temp/ directory\n\n```ts\nconst result = findRemoveSync(\"/temp\", { extensions: [\".bak\", \".log\"] });\n```\n\nthe return value `result` is a json object with successfully deleted files. if you output `result` to the console, you will get something like this:\n\n```\n{\n    '/tmp/haumiblau.bak': true,\n    '/tmp/dump.log': true\n}\n```\n\n### 2. delete all files called 'dump.log' within the /temp/ directory and within its subfolders\n\n```ts\nconst result = findRemoveSync(\"/temp\", { files: \"dump.log\" });\n```\n\n### 3. same as above, but also deletes any subfolders\n\n```ts\nconst result = findRemoveSync(\"/temp\", { files: \"dump.log\", dir: \"*\" });\n```\n\n### 4. delete all \\*.bak files but not file 'haumiblau.bak'\n\n```ts\nconst result = findRemoveSync(\"/temp\", { extensions: [\".bak\"], ignore: \"haumiblau.bak\" });\n```\n\n### 5. delete recursively any subdirectory called 'CVS' within /dist/\n\n```ts\nconst result = findRemoveSync(\"/dist\", { dir: \"CVS\" });\n```\n\n### 6. delete all jpg files older than one hour with limit of 100 files deletion per operation\n\n```ts\nconst result = findRemoveSync(\"/tmp\", {\n  age: { seconds: 3600 },\n  extensions: \".jpg\",\n  limit: 100,\n});\n```\n\n### 7. delete all files with prefix 'filenamestartswith'\n\n```javascript\nconst result = findRemoveSync(\"/tmp\", { prefix: \"filenamestartswith\" });\n```\n\n### 8. apply filter options only for two levels inside the /temp directory for all tmp files\n\n```ts\nconst result = findRemoveSync(\"/tmp\", { maxLevel: 2, extensions: \".tmp\" });\n```\n\nthis deletes any `.tmp` files up to two levels, for example: `/tmp/level1/level2/a.tmp`\n\nbut not `/tmp/level1/level2/level3/b.tmp`\n\nwhy the heck do we have this `maxLevel` option? because of performance. if you care about deep subfolders, apply that option to get a speed boost.\n\n### 9. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?)\n\n```ts\nconst result = findRemoveSync(rootDirectory, { dir: \"*\", files: \"*.*\" });\n```\n\n### 10. delete all files that match a regular expression\n\n```ts\nconst result = findRemoveSync(rootDirectory, { files: \"example[1-3]\", regex: true });\n```\n\nthis deletes files `example1.txt`, `example2.txt`, and `example3.txt`, but not `example8.txt`.\n\n### 11. delete all directories that match a regular expression\n\n```ts\nconst result = findRemoveSync(rootDirectory, { dir: \"^assets_\", regex: true });\n```\n\nthis deletes all directories that start with `assets_`.\n\n## api\n\n### findRemoveSync(dir, options)\n\nfindRemoveSync takes any start directory and searches files from there for removal. the selection of files for removal depends on the given options. and at last, it deletes the selected files/directories.\n\n**arguments**\n\n- `dir` - any directory to search for files and/or directories for deletion (does not delete that directory itself)\n- options - currently those properties are supported:\n  - `files` - can be a string or an array of files you want to delete within `dir`.\n  - `dir` - can be a string or an array of directories you want to delete within `dir`.\n  - `extensions` - this too, can be a string or an array of file extensions you want to delete within `dir`.\n  - `ignore` - useful to exclude some files. again, can be a string or an array of file names you do NOT want to delete within `dir`\n  - `age.seconds` - can be any float number. findRemoveSync then compares it with the file stats and deletes those with modification times older than `age.seconds`\n  - `limit` - can be any integer number. Will limit the number of \u003cb\u003efiles\u003c/b\u003e to be deleted at single operation to be `limit`\n  - `prefix` - can be any string. Will delete any files that start with `prefix`.\n  - `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees.\n  - `test` - advanced: set to true for a test run, meaning it does not delete anything but returns a JSON of files/directories it would have deleted. useful for testing.\n  - `regex` - set to true to treat `files` or `dir` option strings as regular expression patterns.\n\nas a precaution, nothing happens when there are no options.\n\nthe unit tests are good examples on how to use the above arguments.\n\n**returns**\n\nJSON of files/directories that were deleted. For limit option - will only return number of files deleted.\n\n## todo\n\n- add more filtering options (e.g. combinations)\n- have an asynchronous solution\n- use streams instead\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarykitchen%2Ffind-remove","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinarykitchen%2Ffind-remove","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarykitchen%2Ffind-remove/lists"}