You know that dreaded “calculating time remaining” window, wherein you are trying to delete all the files in a folder and the operating system is simply wasting time trying to count the files within as well as estimate the time required to delete it all?
I know it all too well. It can present a problem when trying to quickly free up space or otherwise work efficiently. So naturally, I had to figure something else out when purging masses of data.
Bonus: This method also bypasses any limitation in how long the path/filename can be when trying to delete through Windows Explorer.
Here’s some information about RoboCopy and this scenario:
robocopy <Source> <Destination> [<File>[ ...]] [<Options>]
|<Source>||Specifies the path to the source directory.|
|<Destination>||Specifies the path to the destination directory.|
|<File>||Specifies the file or files to be copied. You can use wildcard characters (* or ?), if you want. If the File parameter is not specified, *.* is used as the default value.|
|<Options>||Specifies options to be used with the robocopy command.|
Options for this use-case
|/purge||Deletes destination files and directories that no longer exist in the source.|
|/mir||Mirrors a directory tree (equivalent to /e plus /purge).|
- The /mir option is equivalent to the /e plus /purge options with one small difference in behavior:
- With the /e plus /purge options, if the destination directory exists, the destination directory security settings are not overwritten.
- With the /mir option, if the destination directory exists, the destination directory security settings are overwritten.
How to empty entire directory contents
Create an empty directory such as
In this scenario, the folder we would like to empty out is
After creating the empty directory, you are ready to erase all the contents of your desired directory like so:
robocopy c:\empty c:\test /purge
robocopy c:\empty c:\test /MIR
* the difference between these two methods will vary only slightly, and will result in something unexpected if you are trying to preserve security permissions on the original directory. Please refer to the above “Remarks” section.
Since the source directory (
C:\empty) is lacking any contents, when Robocopy tries to mirror it, or when robocopy uses it as a purge reference, it simply deletes the contents of the target directory (
I hope this helps you, thanks for reading!