Delete files or empty directories and their empty parents from bottom to up.
import { rmUp } from "rm-up";
await rmUp("generated/template/db/ddl");
await rmUp(["generated/lib/math", "generated/util/helper"], { cwd: "/path/to/project", stop: "." });
rm-up
deletes only files below the options.stop
to prevent unintentional delete operations. If the options.stop
is not provided rm-up
deletes empty directories up to root.
For example:
await rmUp("/root/a/b/c", { stop: "root/x" }); // nothing would be deleted.
await rmUp("/root/a/b/c", { stop: "root/a" }); // "b" and "c" deleted if empty.
rmUp
function deletes files or empty directories and their empty parents from bottom to up.
First deletes given files or directories, then checks whether their parent directories are empty and deletes them if empty.
Then checks parent's parents and so on.
It's direction is reverse of rm -rf
or deltree
command.
Ignores system junk files such as .DS_Store
and Thumbs.db
.
For CLI, please see rm-up-cli.
Why do I need this?
It is most useful when you create a file in a deep path programmatically and want to delete whole created tree, but not sure if other files are added somewhere between by others.
For example you created "generated/template/db/ddl/create.sql
" in your project "/path/to/project
" and want to delete whole created tree.
Example 1
await rmUp("generated/template/db/ddl/create.sql", { cwd: "/path/to/project" });
project project project └─generated/└─generated/└─template/└─template/└─db/ --▶└─db/--▶ └─ddl/└─ddl/└─create.sql└─create.sql
create.sql
file would be deleted, ddl
become empty and deleted, db
become empty and deleted, so template
and generated
directories are deleted likewise.
Example 2
Same options for a little different directory tree:
await rmUp("generated/template/db/ddl/create.sql", { cwd: "/path/to/project" });
project project project └─generated/ └─generated/ ├─generated/ ├─assets/ ├─assets/ └─assets/ └─template/ --▶└─template/--▶ └─db/└─db/└─ddl/└─ddl/└─create.sql└─create.sql
create.sql
file would be deleted, ddl
become empty and deleted, db
become empty and deleted, template
become empty and deleted, but this time generated
directory not become empty because of assets
and not deleted.
▸ Const
default(paths
, __namedParameters?
): Promise
<string
[]>
deprecated
Use named export: import { rmUp } from "rm-up";
/ const { rmUp } = require("rm-up");
Name | Type |
---|---|
paths |
string | string [] |
__namedParameters |
Options |
Promise
<string
[]>
▸ rmUp(paths
, options?
): Promise
<string
[]>
Delete files or empty directories and their empty parents up to stop
path excluding stop
path.
Name | Type | Description |
---|---|---|
paths |
string | string [] |
are the list of directories to be deleted with their empty parents. All paths must be under the same root. (e.g. you can not mix C: and D: in Windows) |
options |
Options |
are options. |
Promise
<string
[]>
rm-up / Options
Options
• Optional
cwd: string
Current working directory to be used with relative input paths.
• Optional
deleteInitial: boolean
Delete target path (bottom directory) even it is non-empty directory. For example even if c
directory of a/b/c
has some files in it, c
will be deleted.
• Optional
dry: boolean
Dry run without deleting any files.
• Optional
force: boolean
If true, no error is thrown if input path is not a directory or does not exists. CWD is used by default.
• Optional
relative: boolean
If true returns paths are relative to cwd, otherwise absolute paths.
• Optional
stop: string
Path to stop searching empty directories up. Stop directory is not included (not deleted).
• Optional
verbose: boolean
If true returns all deleted directories and files. Otherwise returns only paths which delete command is executed agains.
- rm-up-cli: CLI for this API.