Delete Modes and Backup Dir
Deletion is where most sync incidents happen. Use controlled modes and recovery-friendly patterns.
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
| Flag | Purpose |
|---|---|
--dry-run | Preview planned changes |
--delete-before/--delete-during/--delete-after | Deletion timing control |
--backup-dir | Move replaced/deleted files to safety location |
--suffix | Append suffix for backup copies |
Mapping
Delete Timing (Before / During / After)
| Mode | What it does | When to use |
|---|---|---|
--delete-before | Deletes extras before upload | Rare; only when you must guarantee no extras early |
--delete-during | Deletes while transferring | Large datasets where runtime matters |
--delete-after | Deletes after a successful transfer | Safer default for many workloads |
Even with --delete-after, a wrong source can still cause wrong deletes. Preflight checks are still mandatory.
Safer Mirror Pattern
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)
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
Never run destructive sync directly from a path that might be empty due to mount failure or script error.
If your source is a mount (NFS, CIFS, FUSE, etc.), add a mount check before every run.
Safety Checklist
- Confirm source path is mounted and non-empty.
- Run dry-run and inspect delete list.
- Keep
--backup-direnabled for critical datasets.
How Backup Dir Works
When --backup-dir is set, rclone moves files that would be overwritten/deleted into the backup dir.
| Outcome | Without --backup-dir | With --backup-dir |
|---|---|---|
| Destination extra file | Deleted | Moved to rollback prefix |
| Destination file replaced | Overwritten | Previous version moved to rollback |
This gives you an operational rollback path.
Common Pitfalls
| Pitfall | Impact | Prevention |
|---|---|---|
| Sync from wrong source root | Massive remote deletion | Add preflight path sanity checks |
| No rollback location | Slow or impossible recovery | Use timestamped --backup-dir |
| Blind trust in automation | Silent data drift | Alert on unusual delete counts |
Hands-On Practice
- Create a lab destination prefix.
- Upload two files.
- Remove one locally.
- Run a dry-run sync with
--backup-dirand confirm you understand the rollback behavior.
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