Mirror Folders with Robocopy Without Deleting Files
In this article
Robocopy's /MIR option is a powerful way to keep two folders in sync, but it can also delete files in the destination that no longer exist in the source. This guide shows you how to mirror folders safely, with a dry-run step that prevents accidental data loss. You'll learn the exact commands, how to test them, and what to do if something goes wrong.
What You'll Need
- A Windows PC (Robocopy is built into Windows 7 and later).
- Source and destination folder paths (e.g.,
D:\PhotosandE:\Backup\Photos). - Read/write permissions on both folders.
- Optional: a text editor to save a batch file for repeated use.
Step-by-Step
- Open Command Prompt as Administrator. Press
Win, typecmd, right-click “Command Prompt,” and choose “Run as administrator.” This ensures you have the rights to copy all files, including those with restricted permissions. - Understand the /MIR flag.
/MIR(mirror) makes the destination exactly match the source. It copies new and changed files, and deletes files in the destination that are not in the source. This is why it's risky. - Run a dry-run first (critical). Use the
/Lflag to list what would happen without actually copying or deleting anything. Replace the paths with your own:robocopy "D:\Source" "E:\Destination" /MIR /L
This shows a list of files that would be copied, changed, or deleted. Review the list carefully. If you see any files you want to keep, they are missing from the source—do not proceed until you fix that. - Add safety flags. To avoid deleting files, do not use /MIR. Instead, use
/E(copy subdirectories, including empty ones) and/XO(exclude older files) to only copy newer files. This is a one-way sync that never deletes:robocopy "D:\Source" "E:\Destination" /E /XO
This copies new and updated files but leaves extra files in the destination alone. - If you truly need a mirror, use /MIR with a backup. Before running /MIR, ensure you have a full backup of the destination folder. You can use Robocopy itself to copy the destination to a backup location:
robocopy "E:\Destination" "F:\Backup" /E
Then run the mirror command:robocopy "D:\Source" "E:\Destination" /MIR
- Test with a small folder. If you're new to Robocopy, practice on a test folder with a few dummy files. This helps you understand the output and confirm the behavior before touching real data.
- Save your command as a batch file. Create a
.batfile with the command you intend to use. This reduces typos and lets you run the same sync repeatedly. For example, createsync.batwith:@echo off robocopy "D:\Source" "E:\Destination" /E /XO pause
Common Mistakes
- Using /MIR without a dry-run. The biggest mistake. Always run
/Lfirst. - Confusing /E with /MIR. /E only copies, never deletes. /MIR deletes extra files. Know the difference.
- Typing paths incorrectly. A missing backslash or a wrong drive letter can cause Robocopy to target the wrong folder. Double-check paths, especially when using variables.
- Not closing programs that use the destination. If a file is open in another program, Robocopy may skip it or fail. Close all applications that might access the destination.
Tips
- Use /LOG to keep a record. Add
/LOG:sync.logto write the operation details to a file. This helps you review what was done. - Use /R:3 /W:5 to limit retries (3 retries, 5-second wait). This prevents Robocopy from hanging on a locked file.
- Use /NP to suppress progress percentages, making the log cleaner.
- Consider /XD to exclude directories. For example,
/XD "D:\Source\Temp"skips a folder you don't want to sync. - Test with /L and /LOG to save the dry-run output for later review.
Troubleshooting
- Robocopy says “Access Denied.” Run as Administrator, or check folder permissions. Ensure you have read access to the source and write access to the destination.
- Files are skipped because they are in use. Close the application using the file. If it's a system file, you may need to schedule the sync during off-hours.
- The dry-run shows files that would be deleted, but you want them. Do not run /MIR. Instead, copy those files back to the source, or use /E /XO to update the destination without deleting.
- Robocopy exits with a code like 1 or 2. Exit codes are normal: 0 means no files copied, 1 means files copied successfully, 2 means extra files detected (with /MIR). Codes above 7 indicate errors. Look up the specific code online.
- The sync is too slow. Use /MT (multi-threaded) with a number, e.g.,
/MT:16, to speed up copying on multi-core systems.
Frequently Asked Questions
Does /MIR always delete files?
Yes, /MIR deletes files in the destination that do not exist in the source. It also deletes empty folders. That's why it's called a mirror.
Can I undo a Robocopy deletion?
No. Robocopy does not move deleted files to a recycle bin. Once deleted, they are gone. Always have a backup before using /MIR.
What is the difference between /E and /MIR?
/E copies all subdirectories, including empty ones, but never deletes anything. /MIR does the same plus deletes extra files in the destination.
How do I only copy newer files?
Use /E /XO. /XO excludes files that are older than the destination copy, so only newer files are copied.
Can I use Robocopy to sync two folders both ways?
Robocopy is one-way only. For two-way sync, you would need a different tool or run two separate commands, but that risks conflicts. For simple backup, one-way is safer.
Is Robocopy available on Windows 10 and 11?
Yes, Robocopy is included in all modern Windows versions, including Windows 10 and 11.