Deleting very long complicated directory structure in Windows

If you are a programmer, you would have faced this kind of issue at least once in your programming life, a long folder structure got created and you are not able to delete it using Windows delete option nor with DOS commands.

Normally you can delete folders recursively using command "rmdir", for example, "rmdir delete_me /s /q" but if it is really large rmdir simply returns an error "File name too large...".

Root cause: In Windows and DOS, there is a flag "MAX_PATH" which has value 260, means a file path can have only a maximum of 260 characters including the drive and null character("D:\256-character path string"). So, files created by other programs which has more than 260 characters cannot be deleted using Windows or DOS commands.

There are multiple ways to delete these files/folders, like moving few set of folders into the root folder, or writing a script which will recursively delete folders etc. If you don't want to spend time on these try the command "robocopy". Robocopy is a Windows command to copy files from one location to another.

Syntax: robocopy <Source> <Destination> [<File>[ ...]] [<Options>]

There is an optional flag MIR which can be used to mirror a directory tree. We will use this flag to delete the folders recursively, how?

First we will create an empty directory say "c:\empty". Say the folder we want to delete is "c:\delete_me", then issue the command:

robocopy "c:\empty" "c:\delete_me" /MIR
The trick is, robocopy tries to mirror the empty folder; since there are no files or folders in the source directory, it simply deletes the files and folders under target directory recursively! Nice trick, isn't it?

Hope we saved your day! Feel free to drop your comments below.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.

Post a Comment (0)
Previous Post Next Post