View source: R/delete_project_skeleton.R
| delete_project_skeleton | R Documentation |
This function deletes the contents of a project folder, but protects the file from which it is being executed (and the minimal set of ancestor directories required to keep that file). It is intended for cleaning out automatically generated project skeletons while leaving the active script or Quarto/Rmd file intact.
delete_project_skeleton(
project_root,
require_sentinel = TRUE,
sentinel_names = c(".ok-to-prune", ".git", ".gitignore", ".here", "_project.yml",
"_quarto.yml"),
confirm = TRUE,
dry_run = FALSE,
large_n_warn = 10000,
keep_file = NULL
)
project_root |
Path to the project folder whose contents should
be deleted, e.g., |
require_sentinel |
Logical. If |
sentinel_names |
Character vector of sentinel filenames checked
when |
confirm |
Logical. If |
dry_run |
Logical. If |
large_n_warn |
Integer. Threshold number of items that triggers
an additional confirmation prompt. Default is |
keep_file |
Path to the file to protect, or |
Several safety mechanisms are built in to reduce the risk of catastrophic deletion:
Refuses to operate on filesystem roots (e.g., / or C:/).
Optionally requires a sentinel file (e.g., .ok-to-prune)
or other project marker in the target directory.
Displays an interactive confirmation prompt requiring the exact folder name to be typed.
Aborts in non-interactive sessions unless confirm = FALSE
is set explicitly.
Supports a dry-run mode to preview deletions.
Issues an extra confirmation if the number of items to be deleted is very large.
As an additional safeguard, unless keep_file is supplied, the function
must be able to identify the file it is being called from (via
knitr/Quarto, RStudio, or Rscript --file=) so that it never deletes the
running script or its parent directories. It is therefore intended to be
run from a script such as tools/project_creator.qmd, and it aborts if the
current file cannot be determined.
A dry run (dry_run = TRUE) does not ask for confirmation, because it
deletes nothing.
Invisibly, a list with project_root, keep_file, paths (the
top-level paths deleted, or that would be deleted if dry_run = TRUE,
relative to project_root), and n_items (the number of files and
directories they contain, including themselves).
Deletion is irreversible. Always test with dry_run = TRUE first, and
keep require_sentinel = TRUE and confirm = TRUE for normal use.
project <- file.path(tempdir(), "my_project")
create_project_skeleton(project, quiet = TRUE)
keep <- file.path(project, "tools", "project_creator.qmd")
# Preview what would be deleted
delete_project_skeleton(project, keep_file = keep, dry_run = TRUE)
# Delete everything except `keep`. confirm = FALSE skips the typed
# confirmation: only use it when you are sure.
delete_project_skeleton(project, keep_file = keep, confirm = FALSE)
list.files(project, recursive = TRUE)
# From a script inside the project (e.g., tools/project_creator.qmd), the
# running file is detected and protected, so `keep_file` is not needed:
# delete_project_skeleton(project_root = "../")
unlink(project, recursive = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.