To clear a reusable output folder while keeping the folder itself, create a Mac shortcut that removes only its children. This walkthrough uses a fixed folder, disposable text files, and a shell script that checks the folder before clearing it.

The task is to reset one folder between batches of generated files. The example includes ordinary files, a filename containing spaces, a hidden file, and a nested folder. The script removes these contents directly; it does not move them to the Trash. Apple’s published rm manual documents recursive removal and error reporting. Apple’s rm manual

Decide what should remain after clearing

For this workflow, the expected result is an existing, empty folder at the same path. Any subfolders inside it are output too, so they are removed with their contents. If your output directory contains templates or a permanent subfolder structure, this particular script needs a different selection rule before you use it.

The example target is Shortcut Output Demo, directly inside your home folder. Keeping the target fixed makes the shortcut easy to inspect: its behavior does not depend on which Finder window is open or which file happens to be selected.

Requirement Behavior in this walkthrough
Preserve the output folder Only child paths are passed to rm
Include hidden output Bash’s dotglob option includes dot-prefixed names
Handle an already empty folder An empty child list is accepted
Handle a missing folder Stop with an error; do not recreate it
Remove nested output Remove each child folder recursively
Preserve the ability to use Finder’s Put Back Use the Finder alternative below instead

Finder provides a useful alternative when you want items to remain in the Trash. Open the output folder, select the disposable items inside it, and press Command-Delete. Leave the containing folder unselected. Apple documents that items moved to the Trash remain there until it is emptied and can be restored with File > Put Back. Delete files and folders on Mac

Create a disposable folder for the first run

Open Terminal and paste the complete setup block below. It creates only synthetic text files. The initial mkdir must succeed before any files are written, so an existing file or folder named Shortcut Output Demo causes setup to stop instead of reusing its contents.

/bin/bash <<'BASH'
set -eu
target="$HOME/Shortcut Output Demo"

if ! /bin/mkdir "$target"; then
  printf 'Setup stopped: choose an unused demo folder name.\n' >&2
  exit 1
fi

/bin/mkdir "$target/Batch"
printf 'Synthetic output A\n' > "$target/alpha.txt"
printf 'Synthetic output B\n' > "$target/sample output.txt"
printf 'Synthetic marker\n' > "$target/.demo-marker"
printf 'Synthetic nested output\n' > "$target/Batch/nested.txt"
printf 'Created disposable examples in: %s\n' "$target"
BASH

Open your home folder in Finder and locate Shortcut Output Demo. It contains three visible children: alpha.txt, sample output.txt, and Batch. The fourth child, .demo-marker, is deliberately hidden. Inside Batch is nested.txt. These are illustrative test inputs, not existing user files.

If setup stops because the name already exists, leave that existing item alone. Choose another unused demo name and change the target assignment in both setup and the clearing script to match. Do not remove the initial creation check just to get past the collision.

Build the shortcut and preserve its input and output types

In Shortcuts, click the add button to create a shortcut and name it Clear Demo Output. Search the action list for Run Shell Script and add that action. Apple documents creating shortcuts by adding actions, and searching the action list by name. Create a custom shortcut on Mac, Navigate the action list in Shortcuts on Mac

Set the action’s Shell to /bin/bash, then replace its sample commands with the complete clearing script below. This script uses Bash arrays and shopt, so keep that shell selection. It reads its target from the assignment in the script; it does not consume shortcut input, standard input, or command-line arguments.

If scripts are disabled, choose Shortcuts > Settings, open Advanced, and enable Allow Running Scripts. Apple documents this setting for running scripts from shortcuts. Apple’s script-running setting

set -u
target="$HOME/Shortcut Output Demo"

if [[ ! -d "$target" || -L "$target" ]]; then
  printf 'Expected an existing ordinary folder: %s\n' "$target" >&2
  exit 1
fi

if [[ ! -r "$target" || ! -x "$target" ]]; then
  printf 'Cannot list the output folder.\n' >&2
  exit 1
fi

unset GLOBIGNORE
set +f
shopt -u failglob
shopt -s dotglob nullglob

items=( "$target"/* )
for item in "${items[@]}"; do
  if ! /bin/rm -R -- "$item"; then
    printf 'Clearing stopped; some items may remain.\n' >&2
    exit 1
  fi
done

remaining=( "$target"/* )
if (( ${#remaining[@]} != 0 )); then
  printf 'The folder still contains items.\n' >&2
  exit 1
fi

printf 'Folder is empty: %s\n' "$target"

The action’s successful output is text: a line beginning Folder is empty:. It is not a Folder object, a list of deleted files, or a reference to the reusable directory. If you later extend the shortcut, explicitly supply the destination folder to any action that expects a folder. Apple’s action information panel identifies accepted input and resulting output; Control-click an action’s icon and choose Show Info to inspect it. View action input and output information

For a visible completion message, add Show Alert after the script and insert the script’s output variable into its message. Apple documents passing a previous action’s output into Show Alert for inspection. A fixed message saying the folder is empty would provide less useful feedback than displaying the script’s actual result. Use the Show Alert action in a shortcut on Mac

Understand why the folder survives

The assignment items=( "$target"/* ) collects child paths. The loop passes each child separately to rm; it never passes "$target" itself. Recursive removal therefore applies to output subfolders while leaving the reusable parent folder in place.

Two Bash options handle common reset problems. dotglob includes filenames beginning with a dot, and nullglob makes an unmatched pattern expand to nothing. Quoted array expansion preserves filenames containing spaces as individual arguments. These behaviors are documented in Apple’s distributed Bash manual. Bash manual: pathname expansion and arrays

The removal command uses -R for nested folders and -- to end option parsing. It checks each command’s result and stops on failure. Apple’s rm manual specifies a nonzero exit status when an error occurs. Removal is not a transaction: a failure on a later child does not restore children already removed. Apple’s rm manual

The script also rejects a target that is a symbolic link. Keep this example scoped to an ordinary local output folder, with no mounted volumes inside it. Stop any process generating output before clearing: files created during or after the run can invalidate the expectation that the folder remains empty.

Verify the first run and repeated runs

Run the shortcut using the editor’s run button. Check the result against this sequence before adapting the target path. Apple documents testing a custom shortcut from its editor. Test a custom shortcut

  1. Confirm that Shortcut Output Demo still exists in Finder.
  2. Confirm that its visible example files and Batch folder have disappeared.
  3. Confirm that the script returned its empty-folder message; its final check includes hidden children.
  4. Run the shortcut again. An already empty folder should produce the same message.
  5. Rename the empty demo folder temporarily and run again. The script should stop with the existing-folder error. Restore its name afterward.

The clearing script passed a Bash syntax check with bash -n. It has not been executed in macOS Shortcuts for this article, so the disposable first-run sequence remains part of the setup.

If removal fails, inspect the reported item and the folder’s remaining contents before retrying. A missing target, an unreadable directory, and a failed removal are different conditions; the script does not turn them into a success message.

Reuse the folder without assuming collision handling

Once the disposable test behaves as expected, change the target assignment to your chosen output folder. Review its contents first and keep the same contract: everything inside is replaceable output, including hidden items and subfolders.

Clearing a folder removes existing names at that moment. It does not define what a later export or save action does when two new outputs have the same name. Check that action’s own options and description rather than assuming this reset enables replacement, merging, or automatic renaming. Apple provides action descriptions through Show Info. Inspect an action’s behavior

You can also run the saved shortcut from Terminal with the complete command below. Apple documents quoting shortcut names containing spaces and reports that the shortcuts command returns 0 on success or 1 on error. Run shortcuts from the command line

shortcuts run "Clear Demo Output"

Keep clearing and generating output in a deliberate sequence: finish the previous batch, clear the chosen folder, check the result, then create the next batch. That gives the reusable folder a clear purpose and makes repeated runs predictable.

Sources