Reference · Files and processes

delete_file

signature
delete_file(str) -> null | error(str)

What it does

Removes a file under the program's folder, or a folder and everything in it. It gives null when the path is gone, also when there was nothing there, and an error value when the path leaves the folder or names the folder itself.

Examples

write_file("tmp/a.txt", "a")print(delete_file("tmp/a.txt"))print(read_file("tmp/a.txt") ?? "gone")
output
nullgone

Edge cases

print(delete_file("nope.txt"), delete_file("nope.txt"))write_file("tmp/a/b.txt", "b")write_file("tmp/c.txt", "c")print(delete_file("tmp"), read_file("tmp/c.txt") ?? "gone")print(delete_file("."), delete_file(""))print(delete_file("../x"), delete_file("/tmp/x"))
output
null nullnull goneerr("delete_file: path must be inside the program's folder: .") err("delete_file: path must be inside the program's folder: ")err("delete_file: path must be inside the program's folder: ../x") err("delete_file: path must be inside the program's folder: /tmp/x")

Errors

A path outside the folder, or one of the program's own files or a folder holding one, is an error value, which ? passes on; a path that is not a string is a runtime error.

let r = delete_file("..")print(is_err(r), r.error)
output
true delete_file: path must be inside the program's folder: ..
db.open("app.db")print(delete_file("app.db"), delete_file("MAIN.qwe"))
output
err("delete_file: will not delete app.db: it is the database the program has open") err("delete_file: will not delete MAIN.qwe: it is one of the program's own files")
this one fails
print(delete_file(1))
output
line 1: `delete_file` expects a str as argument 1, got int

Chaining

write_file("a.txt", "a")write_file("b.txt", "b")print(["a.txt", "b.txt"] | map(delete_file))print("a.txt" | read_file ?? "gone")
output
[null, null]gone

See also

write_file, read_file, save_upload

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On delete_file(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo