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:
Syntax
robocopy <Source> <Destination> [<File>[ ...]] [<Options>]
Parameters
Parameter | Description |
<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). |
Remarks
- 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 C:\empty
In this scenario, the folder we would like to empty out is C:\test
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
or
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 (C:\test
)
I hope this helps you, thanks for reading!
5 Comments
Michael Mavr · December 17, 2019 at 11:58 am
Perfect Solution
Thank you very..!!!!
Paul W · December 6, 2019 at 6:02 am
excellent. Fast and elegant solution, and no extra software required. Thanks very much.
Brandon Wooten · August 8, 2018 at 2:24 am
you should try long path tool…
it’s very useful for me.
Ross Gram · May 19, 2018 at 10:35 am
I would like to suggest you, try LongPathTool program to resolve this issue. This tool is very helpful to resolve the issue.
Eric · March 5, 2018 at 1:25 pm
But is there a way to delete the files in the destination and NOT copy files that exist in he source and not he destination?