delete_project_skeleton: Delete a project skeleton while protecting the running file

View source: R/delete_project_skeleton.R

delete_project_skeletonR Documentation

Delete a project skeleton while protecting the running file

Description

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.

Usage

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
)

Arguments

project_root

Path to the project folder whose contents should be deleted, e.g., "../" from tools/project_creator.qmd in the project. There is no default, so that nothing is deleted unless you name the folder.

require_sentinel

Logical. If TRUE (default), at least one of the files listed in sentinel_names must exist in the project root, otherwise the function aborts.

sentinel_names

Character vector of sentinel filenames checked when require_sentinel = TRUE. Defaults include .ok-to-prune, .git, .here, ⁠_project.yml⁠, and ⁠_quarto.yml⁠.

confirm

Logical. If TRUE (default), asks the user to confirm the deletion interactively by typing the folder name. In non-interactive sessions this must be set to FALSE to proceed.

dry_run

Logical. If TRUE, only reports what would be deleted without actually deleting files. Default is FALSE.

large_n_warn

Integer. Threshold number of items that triggers an additional confirmation prompt. Default is 10000.

keep_file

Path to the file to protect, or NULL (default) to detect the file the function is being called from. Supply it to run the function from the console or a script whose location cannot be detected. It must exist; if it is outside project_root, nothing is protected.

Details

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.

Value

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).

Warning

Deletion is irreversible. Always test with dry_run = TRUE first, and keep require_sentinel = TRUE and confirm = TRUE for normal use.

Examples

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)


psychdsish documentation built on Nov. 29, 2026, 5:07 p.m.