If you ask robocopy to destroy the destination, then it will destroy the destination

A customer reported that Explorer was not showing a folder on their hard drive that they were sure was there.

I asked them to check whether the folder really was there, by going to a command prompt and using the dir /a command.

Turns out the folder really was gone.

The customer went back and retraced their steps and reconstructed what happened.

First, the customer created a folder on their D: called D:\backups\fdrive\spreadsheets.

Next the customer copied two files from their F: drive to the D:\backups\fdrive\spreadsheets folder.

So far so good.

Next, the customer wanted to copy their entire F: drive to the D:\backups\fdrive folder, so they performed the following command:

robocopy /MIR F: D:\backups\fdrive

The customer let this command run for a while, but then the operation started encountering Access denied errors, so they hit Ctrl+C to stop the robocopy command.

At this point, the customer noticed that the spreadsheets folder was gone.

The customer theorized, "I suspect that what happened is that robocopy was matching the directory structure of the F: drive against the directory structure of D:\backups\fdrive, and since my important spreadsheet files weren't present in the F:\spreadsheets folder on the source, it deleted them from the destination. If I had let the copy run to completion, it presumably would have eventually copied the files from their location on the F: drive to the corresponding subdirectory of D:\backups\fdrive."

The customer continued, "From a user perspective, it seems that I should have really been alerted by robocopy that the target folder (in this case, D:\backups\fdrive) wasn't empty, and it should have asked for confirmation that I didn't really want to lose those files (which I didn't)."

Well, um, yeah, because that's what the /MIR option means.

       /E :: copy subdirectories, including Empty ones.
   /PURGE :: delete dest files/dirs that no longer exist in source.
     /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

The /MIR option means that the destination folder should be an exact copy of the source folder. The documentation points out that this is the same as /E (copy recursively) combined with /PURGE (remove anything from the destination that is not present in the source).

The /PURGE behavior is by definition destructive.

If that's not what you want, then don't pass the /MIR flag.

Comments (21)
  1. The MAZZTer says:

    If you use /mir along with /xf or /xd to exclude files or folders, not only does it exclude copying those files and folders (like you’d expect), but if the files/folders are present in the destination they are NOT removed as you might expect. This occurs even with full paths to the source files/folders specified (so it’s not just matching on a filename if you just provide a filename without a path). It makes sense if you think about it, since it’s excluding those items from ALL operations, not just the copy.

  2. Joshua says:

    Yet another case of continuing after an error and trying to understand the resulting deranged state. Had they addressed the Access Denied errors and redid the robocopy command they would have no problem.

    1. Ted M says:

      If they were mirroring a volume I’d hazard a guess that it was the NTFS System Volume Information folder that was throwing Access Denied errors

  3. Cesar says:

    That sounds like rsync’s –delete-before or –delete-during. Does robocopy have the equivalent of rsync’s –delete-after? That would avoid this problem, at the cost of potentially running out of disk space (but at least, if it runs out of disk space, nothing would be deleted).

  4. Adrian says:

    I’d never heard of robocopy and at first I figured it was a third-party application. On a lark, I just typed robocopy in a command prompt and discovered that it comes with Windows. Since I’d given no options or parameters, robocopy printed some the help text that had a very explicit warning:

    “**** /MIR can DELETE files as well as copy them !”

    1. Brian says:

      It was a very useful add-on that first showed up in the NT4 resource kit way back in the day. It’s been built in to Windows since Vista (https://en.wikipedia.org/wiki/Robocopy).

    2. voo says:

      It is still the best tool for copying large data in scripts these days. Which is kind of sad all considered because goodness the flags are anything but intuitive.

      I’m sure there are great third party PowerShell modules available these days though – I should look into those.

      1. Stephen Donaghy says:

        I think “best” is quite a subjective term. If you just need to mirror a directory, robocopy is probably the best choice, but if you’re trying to copy files with certain properties (like say all image files older than a certain date), powershell will win hands down.

        Basically, if it’s straight up copying and nothing else, use robocopy – for anything else, use powershell.

        1. voo says:

          Well I do refer to PowerShell, the problem is that the built-in commands have serious limitations/bugs that require more work than it should take (copy-item recursively with exclude not working correctly is a PITA).

          One can indeed write a PowerShell accept that provides the wanted behaviour but that’s not as easy as one would hope and if I’m on foreign sever I won’t have the option.

    3. Dave says:

      Hmm, never heard of it either, so I ran it to have a look and got the info page/printout. Just wondering, was whoever wrote it an ex-VMS programmer?

  5. ta.speot.is says:

    I’ve always wondered how these sorts of support cases make it to Raymond. Is there some sort of arrangement where all Windows developers are on the line for helping customer support representatives, and it’s round-robin? So you’d get the good ones, and then you’d get … rtfm.

    1. Scarlet Manuka says:

      Don’t forget, the original query was “I definitely have this folder on my hard drive, but Explorer isn’t showing it for some reason, why is Explorer doing this?” That sounds like it would be much more up Raymond’s alley than “what does this option do again?”

      1. Yup, that’s how it started.

      2. Tom West says:

        I have to wonder just what sort of support contract the customer had.

        It’s sort of like praying and having God answer, “Yes, how can I be of assistance?”

  6. You need to be careful with what you ask for. Robocopy has some very dangerous options

  7. The_Assimilator says:

    While I don’t have much sympathy for users who don’t read the documentation, I do feel that having a util with “copy” in its name implicitly performing deletions isn’t the best thing in the world.

    1. SI says:

      Using that logic, you shouldn’t be allowed to use copy /Y either because it deletes an existing file.

    2. voo says:

      “Implicitly” is a weird description for behaviour that’s only evoked if you explicitly pass a parameter to the command.

  8. Mark Sowul says:

    I think a higher level sadness is that the recycle bin is an explorer-only construct…
    Would be nice if deleting things via the command line resulted in moving to the recycle bin by default. I know a lot of stories that would have had happier endings if that were the case.

    1. Zan Lynx' says:

      Window’s File History, Volume Shadow Copies, and ordinary backups.

  9. Ian Yates says:

    I noticed with robocopy the other day that if you want help, but don’t provide the /? switch, you just get this

    ROBOCOPY :: Robust File Copy for Windows

    Started : Thursday, 22 December 2016 11:22:27 PM
    Simple Usage :: ROBOCOPY source destination /MIR

    source :: Source Directory (drive:\path or \\server\share\path).
    destination :: Destination Dir (drive:\path or \\server\share\path).
    /MIR :: Mirror a complete directory tree.

    For more usage information run ROBOCOPY /?

    **** /MIR can DELETE files as well as copy them !

    (tried on Win Server 2008 R2 and Win 8.1)

    Yes it has the warning, but some users would be just oh so happy to get the simple usage working and blindly put the /MIR in there without thinking. Probably not the best example to include as the simple usage since it’s no longer just a harmless *copy*

Comments are closed.

Skip to main content