The "Attempt to delete a directory or file that is not empty" error occurs when Robocopy fails to clear the source folder after a move operation. This usually happens due to permission issues, open files, or insufficient privileges. To resolve it, ensure no files are locked, run the command as an administrator, and verify that the destination copy succeeded before deletion.
Understanding the Error Cause
Robocopy is a powerful command-line utility designed for robust file copying and moving. When you use the/MOVE switch, Robocopy performs two distinct actions: it copies all files and folders from the source to the destination, and then it attempts to delete the original source content. The error message indicates that the second step has failed. Specifically, the system cannot remove the source directory because it still contains items that the current process does not have permission to delete, or which are currently in use by another application.
This behavior is distinct from simple copying. If you were using only /COPYALL, the source would remain untouched. However, /MOVE implies a transfer of ownership and physical location. The failure at the deletion stage suggests that while the data was successfully written to the new location, the cleanup phase encountered an obstacle. Common culprits include residual read-only attributes on files, active locks from antivirus software scanning the newly copied files, or user account control restrictions preventing the deletion of system-protected directories.
Permissions and Ownership Issues
One of the most frequent causes involves access control lists (ACLs). If the source folder contains subdirectories with specific permissions that differ from your current user context, Robocopy might succeed in copying the files but fail to delete the empty shell of the folder structure afterward. Additionally, if the source is located on a network drive with restrictive policies, the network session might drop during the large file transfer, leaving orphaned files that prevent folder deletion. Ensuring that you have full control over both the source and destination paths is critical. You can check this by right-clicking the folder, selecting Properties, and navigating to the Security tab to verify your user account has "Full Control."File Locks and Antivirus Interference
Another significant factor is file locking. If any file within the source directory is open in another program, such as a text editor, media player, or even an index service like Windows Search, the operating system will block its deletion. Furthermore, real-time antivirus protection often scans files immediately after they are created or modified. If the antivirus engine holds a lock on a newly copied file for a brief period, Robocopy may attempt to delete the parent folder before the scan completes, resulting in the error. Disabling real-time protection temporarily or adding an exclusion for the source and destination folders can help isolate this issue.Step-by-Step Resolution Guide
To effectively resolve this error and complete your file move operation, follow these diagnostic and corrective steps. These methods address the most common technical barriers without requiring third-party tools.- Run as Administrator: Always execute the Command Prompt or PowerShell window with elevated privileges. Right-click the start menu icon and select "Terminal (Admin)" or "Command Prompt (Admin)." This ensures Robocopy has the highest level of authority to bypass standard permission checks during the deletion phase.
- Close Open Applications: Before running the command, close all applications that might be accessing files in the source directory. Pay special attention to background processes like cloud sync clients (OneDrive, Dropbox) or email clients that might be indexing the folder.
- Verify Destination Integrity: Manually check the destination folder to ensure all files were copied correctly. If the copy failed partially, Robocopy might behave unpredictably during the move phase. It is safer to retry the copy first before attempting a move.
- Use /E and /MOVE Together: Ensure your command includes the
/Eswitch to copy subdirectories, including empty ones, along with/MOVE. The correct syntax typically looks like:robocopy "SourcePath" "DestPath" /MOVE /E. - Check for Read-Only Attributes: Run the command
attrib -R "SourcePath\*.*" /S /Dbefore the move. This removes read-only flags that can sometimes block deletion operations, although Robocopy usually handles this automatically.
Alternative Approaches
If Robocopy continues to fail despite these steps, consider alternative strategies. You can perform the operation in two separate steps: first, use/COPYALL to copy all files and attributes without deleting the source. Once verified, manually delete the source folder using File Explorer or the rmdir /s /q command. This separation allows you to diagnose whether the issue lies in the copying process or the deletion process.
Another option is to use PowerShell cmdlets like Copy-Item followed by Remove-Item. While less efficient for massive datasets than Robocopy, PowerShell provides better error handling and logging capabilities through its object-oriented pipeline. For recursive copying tasks where moves are not strictly necessary, refer to How to Use Robocopy for Recursive File Copying in Tech Support to learn how to optimize the copy phase alone.
Troubleshooting Specific Scenarios
In some cases, the error persists due to symbolic links or junction points. If your source folder contains shortcuts to other locations, Robocopy may struggle to resolve them during deletion. Inspect the source directory for any symlinks using thedir /al command in the command prompt. Removing these links before the move operation can prevent confusion in the deletion routine.
Additionally, if you are moving data to or from a portable USB drive, be aware of potential filesystem inconsistencies. As noted in discussions about portable drives, hidden system files can interfere with operations. See Unaccountably, Folder Robocopied to Portable USB Drive on Laptop PC Becomes System + Hidden - What to Do? for insights on handling hidden system attributes that might block standard deletion commands.
Frequently Asked Questions
Why does Robocopy delete files but leave folders?
This happens when individual files are deleted successfully, but the parent folder remains because it contains hidden system files or has restricted permissions. Check for hidden files using dir /ah and ensure your user account has full control over the root directory.
Can I recover files if the move fails halfway?
If the move fails, the destination folder should contain the copied files, while the source folder remains intact. Verify the destination integrity manually. If files are missing, re-run the command with the /MIR switch instead of /MOVE to mirror the directory structure without immediate deletion risks.
Does antivirus software cause this error?
Yes, real-time scanning can lock files immediately after they are copied, preventing Robocopy from deleting the source. Temporarily disabling real-time protection or adding exclusions for the source and destination folders can resolve this interference.
How do I force delete a non-empty folder via command line?
You can use the rmdir /s /q command to force delete a folder and its contents silently. However, this bypasses Robocopy's safety checks and should only be used if you are certain the data has been safely backed up elsewhere.
What is the difference between /MOVE and /MIR?
The /MOVE switch copies files and then deletes the source, preserving the directory structure. The /MIR switch mirrors the source to the destination, meaning it also deletes files in the destination that no longer exist in the source. Use /MOVE for transfers and /MIR for backups.