rclone Rmdirs
rclone rmdirs finds and removes all empty directories under the given path, recursively. Like rmdir, it only touches empty directories — directories containing files are left untouched.
Run rmdirs after delete operations to clean up the empty directory skeletons that delete leaves behind. It's completely safe — it never removes directories that contain files.
Basic Syntax
rclone rmdirs REMOTE:PATH [flags]
# Remove all empty directories under a path
rclone rmdirs remote:my-bucket/old-data
# Include the root path itself if empty
rclone rmdirs remote:my-bucket/old-data --leave-root=false
Key Flags
| Flag | Description |
|---|---|
--leave-root | Don't remove the root directory itself (default: true) |
--verbose / -v | Show directories being removed |
--dry-run / -n | Preview without executing |
--log-file PATH | Write log to file |
Practical Examples
Post-Delete Cleanup
# Step 1: Delete files matching a pattern
rclone delete remote:my-bucket/logs --min-age 90d --include "*.log"
# Step 2: Clean up empty directories left behind
rclone rmdirs remote:my-bucket/logs -v
Clean Up After Move Operations
# Move files to archive
rclone move /srv/exports remote:archive/exports --progress
# Clean up empty source directories
rclone rmdirs /srv/exports -v
Preview Before Cleanup
# See what would be removed
rclone rmdirs remote:my-bucket/old-project --dry-run -v
Automated Cleanup Script
#!/bin/bash
LOG="/var/log/rclone/rmdirs-$(date +%Y-%m-%d).log"
# Remove empty dirs across multiple paths
for path in logs temp staging; do
rclone rmdirs "remote:my-bucket/${path}" \
--log-file "$LOG" --log-level INFO -v
done
rmdirs vs rmdir vs purge
| Command | Scope | Recursive | Safe (files untouched) |
|---|---|---|---|
rmdirs | All empty dirs under path | ✅ | ✅ |
rmdir | Single directory | ❌ | ✅ |
purge | Everything | ✅ | ❌ |
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Expecting file deletion | rmdirs only removes empty directories | Use delete for files |
--leave-root default is true | Root directory remains even if empty | Add --leave-root=false if you want total cleanup |
| Object storage "directories" | Placeholder objects may prevent removal | Delete placeholder objects first |
What's Next
Examples with Output
1. Clean up all empty folders recursively
Scan the entire remote and delete every directory that has no files. Command:
rclone rmdirs gdrive:Archives -v
Output:
2024/01/15 12:00:00 INFO : 2022/temp: Removed empty directory
2024/01/15 12:00:00 INFO : 2022: Removed empty directory
2. Leave specific folders alone
Prevent deletion of a directory even if it's empty (e.g., a required mount point). Command:
rclone rmdirs remote:bucket --exclude "keep-me/"
Output:
(Scanning all folders except 'keep-me')
3. Dry run of recursive cleanup
Preview the total number of directories that would be removed. Command:
rclone rmdirs remote:test --dry-run -v
Output:
2024/01/15 12:00:00 NOTICE: folder1: Not rmdir as --dry-run is set
2024/01/15 12:00:00 NOTICE: folder2: Not rmdir as --dry-run is set
4. Deep cleanup including parent folders
Ensure that if a folder becomes empty AFTER its children are deleted, it also gets removed. Command:
rclone rmdirs remote:nested/deep/path --leave-root=false
Output:
(Recursive removal of siblings and parents if empty)
5. Combine with delete for total workspace cleanup
Remove old files and then sweep away the empty shells. Command:
rclone delete remote:old-project --min-age 1y && rclone rmdirs remote:old-project
Output:
(Audit trail of file deletions followed by directory removal)