Skip to main content

Delete Modes and Backup Dir

Deletion is where most sync incidents happen. Use controlled modes and recovery-friendly patterns.

Learning Focus

Learn how to mirror safely without turning a mount failure or a wrong path into mass deletion.

Concept Overview​

rclone sync makes the destination match the source. That implies deletions when:

  • The destination contains extra files not present in source.
  • The source path is wrong, empty, or not mounted.

The goal is not "never delete". The goal is "delete predictably and recoverably".

Key Safety Flags​

FlagPurpose
--dry-runPreview planned changes
--delete-before/--delete-during/--delete-afterDeletion timing control
--backup-dirMove replaced/deleted files to safety location
--suffixAppend suffix for backup copies

Mapping​

Delete Timing (Before / During / After)​

ModeWhat it doesWhen to use
--delete-beforeDeletes extras before uploadRare; only when you must guarantee no extras early
--delete-duringDeletes while transferringLarge datasets where runtime matters
--delete-afterDeletes after a successful transferSafer default for many workloads
note

Even with --delete-after, a wrong source can still cause wrong deletes. Preflight checks are still mandatory.

Safer Mirror Pattern​

safe-mirror.sh
rclone sync /srv/site remote-prod:site/current \
--delete-after \
--backup-dir remote-prod:site/rollback/$(date +%F-%H%M) \
--suffix ".bak" \
--log-file /var/log/rclone-safe-mirror.log

Preflight Guardrails (Must Have)​

preflight-guards.sh
set -euo pipefail

SRC="/srv/site"
DST="remote-prod:site/current"

test -d "$SRC" || { echo "missing source: $SRC"; exit 1; }
test "$(ls -A "$SRC" | wc -l)" -gt 0 || { echo "source empty: $SRC"; exit 1; }
rclone lsd "${DST%/*}" >/dev/null

rclone sync "$SRC" "$DST" --dry-run -vv
warning

Never run destructive sync directly from a path that might be empty due to mount failure or script error.

danger

If your source is a mount (NFS, CIFS, FUSE, etc.), add a mount check before every run.

Safety Checklist​

  1. Confirm source path is mounted and non-empty.
  2. Run dry-run and inspect delete list.
  3. Keep --backup-dir enabled for critical datasets.

How Backup Dir Works​

When --backup-dir is set, rclone moves files that would be overwritten/deleted into the backup dir.

OutcomeWithout --backup-dirWith --backup-dir
Destination extra fileDeletedMoved to rollback prefix
Destination file replacedOverwrittenPrevious version moved to rollback

This gives you an operational rollback path.

Common Pitfalls​

PitfallImpactPrevention
Sync from wrong source rootMassive remote deletionAdd preflight path sanity checks
No rollback locationSlow or impossible recoveryUse timestamped --backup-dir
Blind trust in automationSilent data driftAlert on unusual delete counts

Hands-On Practice​

  1. Create a lab destination prefix.
  2. Upload two files.
  3. Remove one locally.
  4. Run a dry-run sync with --backup-dir and confirm you understand the rollback behavior.
practice-delete-safety.sh
mkdir -p /tmp/rclone-del && echo "a" > /tmp/rclone-del/a.txt && echo "b" > /tmp/rclone-del/b.txt
rclone sync /tmp/rclone-del remote-prod:labs/delete-safety/current --progress
rm /tmp/rclone-del/b.txt
rclone sync /tmp/rclone-del remote-prod:labs/delete-safety/current --dry-run -vv \
--delete-after \
--backup-dir remote-prod:labs/delete-safety/rollback/$(date +%F-%H%M) \
--suffix .bak

What's Next​