Skip to main content

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.

Quick Summary

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​

FlagDescription
--leave-rootDon't remove the root directory itself (default: true)
--verbose / -vShow directories being removed
--dry-run / -nPreview without executing
--log-file PATHWrite 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​

cleanup-empties.sh
#!/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​

CommandScopeRecursiveSafe (files untouched)
rmdirsAll empty dirs under path✅✅
rmdirSingle directory❌✅
purgeEverything✅❌

Common Pitfalls​

PitfallConsequencePrevention
Expecting file deletionrmdirs only removes empty directoriesUse delete for files
--leave-root default is trueRoot directory remains even if emptyAdd --leave-root=false if you want total cleanup
Object storage "directories"Placeholder objects may prevent removalDelete 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)