Robocopy’s /PURGE option deletes files and directories at the destination that no longer exist in the source, effectively mirroring the source structure by removing orphaned items. The /MOVE option transfers files and then deletes them from the source after successful copying, making it suitable for archiving or relocating data rather than simple duplication.
Distinguishing Between /PURGE and /MOVE
Many users confuse these two flags because both result in file deletion, but they operate on opposite ends of the copy process. Understanding the distinction is critical to preventing accidental data loss during backup or migration tasks. /PURGE is a synchronization tool that cleans up the destination, while /MOVE is a transfer tool that empties the source.
When you use /PURGE, Robocopy compares the destination against the source. Any file present in the destination but missing from the source is marked for deletion. This is useful when you want the destination to be an exact mirror of the source, ensuring that deleted files do not linger on the backup drive. However, if you run this command incorrectly, you might delete important data from your target location.
In contrast, /MOVE works by copying the file to the destination first. Once the copy operation confirms success, Robocopy deletes the original file from the source directory. This flag applies to both files and directories. It is ideal for scenarios where you are moving large datasets from one server to another and want to free up space on the original machine immediately after the transfer completes. For more complex directory structures, see Avoiding Infinite Subfolder Duplication with Robocopy on Windows.
How /PURGE Works in Practice
The /PURGE switch tells Robocopy to purge all destination files and folders that do not exist in the source. This includes empty directories as well. If your source folder contains subdirectories that have been removed, /PURGE will also remove those empty subdirectories from the destination.
This behavior is particularly powerful for maintaining clean backups. Without /PURGE, if you delete a file from your source, it remains in your backup. Over time, this leads to "backup rot," where obsolete files consume storage space. By including /PURGE, you ensure that your backup reflects the current state of your source data precisely.
However, caution is required. If you accidentally point the source parameter to an empty folder, /PURGE will delete everything in the destination. Always verify your source and destination paths before executing commands with this flag. It is advisable to test with a small subset of data first. You can also use the /L (List only) flag to preview what would be deleted without actually performing the action.
Understanding /MOVE Behavior
The /MOVE option combines copying and deleting into a single atomic operation per file. It does not just move directories; it moves the contents. When applied to a directory, Robocopy copies all files within that directory to the destination. After each file is successfully copied, the original is deleted.
One key difference between /MOVE and standard Windows move operations is how it handles open files. If a file is locked by another process, Robocopy may fail to copy it, and thus it will not be deleted from the source. This prevents partial moves that could leave data in an inconsistent state. In such cases, check the log file for errors like "Access Denied" or "File in Use."
Another consideration is network stability. Since /MOVE relies on successful completion of the copy before deletion, network interruptions can lead to duplicates if the script retries without proper error handling. Ensure that your Robocopy command includes retry parameters like /R:2 and /W:5 to handle transient network issues gracefully. For insights on handling persistent copy issues, refer to Robocopy Always Copies Directory, Even When Old Not.
Safety Precautions and Best Practices
Before running any Robocopy command with destructive options, always create a full backup of your destination. This provides a safety net in case of misconfiguration. Additionally, use the /E flag to copy subdirectories, including empty ones, unless you specifically want to avoid them. Combining /E with /PURGE ensures that the entire directory tree is synchronized.
Logging is essential for troubleshooting. Use the /LOG+ parameter to append output to a text file. Reviewing the log helps you understand exactly which files were deleted or moved. This is especially helpful when dealing with large numbers of files where manual verification is impractical.
If you encounter issues where the destination appears inaccessible or invisible after operations, check your permissions and path syntax. Sometimes, hidden attributes or system restrictions can interfere with Robocopy's ability to write or delete files. For troubleshooting visibility issues, check Robocopy Destination Invisible on Windows 10: Troubleshooting.
Frequently Asked Questions
Does /PURGE delete files from the source?
No, /PURGE only affects the destination. It removes files from the destination that are not present in the source. The source directory remains completely untouched by this option.
Can I use /MOVE to move entire folders?
Yes, /MOVE moves files and empties the source directories. However, it does not rename the top-level directory itself; it clears its contents. To move the folder structure entirely, you may need to combine it with other commands or scripts.
What happens if a file is locked during a /MOVE operation?
If a file is locked, Robocopy cannot copy it, so it will not delete the original from the source. The file remains in place, and the operation continues with other files. Check the log for specific error codes related to access denial.
Is there a risk of data loss with /PURGE?
Yes, if you specify the wrong source directory, /PURGE can delete valid data from your destination. Always double-check your source path and consider using the /L flag to preview changes before executing the actual command.
How do I prevent /MOVE from deleting files if the copy fails?
/MOVE inherently waits for successful copy completion before deletion. If the copy fails due to errors, the file is not deleted from the source. Ensure you have adequate retry settings (/R and /W) to handle temporary failures.