65

When importing a project into eclipse, it somehow started creating recursive versions of the directory.

Now, when I try to delete anything, I get the message " the file name you specify is too long."

I can't delete it from the command shell. I can't delete it from explorer. I can't do ANYTHING with it. I have no idea how many copies of it Eclipse created. Eclipse was running for like a minute before I realized something was wrong and I cancelled the operation.

I can get the properties of the top level folder, and it says it contains 497 folders.

Question 1: how can I delete all this stuff in Windows?

Question 2: What the hell was eclipse thinking?

4
  • Starting a bounty hoping for a better/easier/faster solution. Alternately, someone fix eclipse so it will recognize a stupid recursive copy operation.
    – Adam Davis
    Feb 2, 2011 at 21:32
  • 3
    technet.microsoft.com/en-us/sysinternals/bb896768 and map some of the deeper folder somewhere else. Alternatively write a 4 line function in java and delete it; static void cleanDir(File dir) { for (File f:dir.listFiles()){ if (f.isDirectory()){ cleanDir(f); } f.delete(); } } ... which reminds an ant task might do as well?
    – bestsss
    Feb 7, 2011 at 23:23
  • Have you tried deleting it from code? Feb 8, 2011 at 15:59
  • It can help you with deleting files from directories with too long names: use SHIFT + DEL. I had the similar problem and it helped me.
    – jellonek
    Aug 1, 2014 at 11:46

20 Answers 20

171
robocopy empty_dir base_nested_dir /purge

Example: If you have a folder path that's too long such as:

D:\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles\BackupFolder\Copy\MyFiles

follow these instructions to delete it.

  1. Create an empty folder, such as C:\Users\Name\Desktop\temp.
  2. Open a Command Prompt window.
  3. Type robocopy C:\Users\Name\Desktop\temp D:\BackupFolder /purge

Note: If there are spaces in either path in Step 3, the path must be inclosed by quotation marks.

8
  • 3
    This is nice in that you don't manually have to rename things or download extra tools. Just create an empty directory, and tell robocopy to purge everything in the target directory which isn't in the empty directory. Handy. Jan 12, 2011 at 21:26
  • Sometimes you need to run step 3 more than one time to actually delete all files and dirs from target dir.
    – Kamil Szot
    Nov 13, 2014 at 8:37
  • Found a scenario where this doesn't work! I was so hopeful, but eventually I actually managed to crash robocopy.exe. I honestly can't tell you where this ludicrously long path came from, but it's too long even for robocopy.
    – Tom W
    Feb 10, 2015 at 17:25
  • I tried may other ways to delete these files: map network drive, subst, zip. None worked. This worked!
    – downeyt
    Apr 14, 2015 at 23:11
  • 6
    This should be the answer
    – Axel
    Apr 23, 2016 at 17:48
90

7Zip is the ultimate solution if you are not comfortable with command-line.

  1. Open 7Zip File Manager
  2. Navigate to your file
  3. Shift-Delete your file

Cheers!

11
  • 2
    I got a directory not empty... just like rimraf
    – Oliver
    May 29, 2015 at 15:08
  • 1
    Make sure you directory is not open in any window and that any file in that directory is not open.
    – Swanidhi
    May 29, 2015 at 15:39
  • 2
    This is the easiest solution. I love being forced to use Windows for my new job with fun things like this to solve. Jul 3, 2015 at 10:42
  • 3
    You are a genius. This is the easiest solution
    – Rakshith
    Aug 18, 2015 at 9:40
  • 1
    Works also with WinRar, rar and delete on
    – iss42
    Aug 27, 2015 at 14:42
19
+500

I had a problem similar to this with Eclipse: trying to import a project using Subclipse, it generated a deeply nested set of directories which I couldn't delete.

I did a couple of things, firstly I edited the directory names down to the shortest length possible (actually I think this was pointless). Secondly, I traversed the directory tree as deeply as possible and then cut and pasted the branch out (which makes it possible to delete the original branch), and then repeated the process.

@Charlie and @Tomalek's solutions look like they might be more elegant. I don't plan on repeating the experience to find out though.

3
  • This might be the most inelegant, but I tried the rest. It took a few minutes, but it solved it. And, that's exactly what happened to me. . .I think I was doing an import -> SVN -> checkout. I'm not going to try to reproduce it to be sure, though.
    – Baltimark
    Feb 15, 2009 at 16:42
  • Glad my experience was of use to you :-) Feb 15, 2009 at 16:53
  • 1
    I wouldn't use it. Practically, I'd first have to check if there are 5 files with such long names of 500. It is always better to deal with such issues using either command line or a tool like 7-Zip File Manager.
    – Swanidhi
    Sep 25, 2015 at 20:08
12

As to Q1)

Use subst to short-cirquit the paths:

subst Q: C:\very\long\path\created\by\eclipse
Q:
del *.*

As to Q2)

Long paths are less of a problem in other OSes, and there are Windows API functions that can deal with paths longer than 255 characters, so they just did it, I guess.

5
  • 1
    my directory list goes. . . dirname -> dirname -> dirname -> . . . about 500 times. I can remap "q" to any part of it, but when I try to delete "q" it has the same issue.
    – Baltimark
    Feb 15, 2009 at 16:14
  • 1
    Then go on with the next drive letter. Look here: stackoverflow.com/questions/236533
    – Tomalak
    Feb 15, 2009 at 16:54
  • Ok tomalak, you're saying to keep adding drive letters until I get a path short enough to delete, and then "delete from the bottom", if you will. Similar idea to Ian's. Might be more efficient, time-wise. Thanks.
    – Baltimark
    Feb 17, 2009 at 15:31
  • @Baltimark: Yes, exactly. If such a situation arises for me, which happens once in a while, I usually deal with it using a small recursive VBScript that handles the ugly part for me and keeps track of all the stacked drive letters. For a one-off situation, doing it manually can be enough as well.
    – Tomalak
    Feb 17, 2009 at 15:51
  • 2
    btw remove the drive using subst Q: /D Aug 21, 2015 at 12:35
11

If you need to deal with paths longer than MAX_PATH, you'll have to use the Unicode versions of the Windows file APIs (e.g. DeleteFileW) and use the \\?\ prefix. You can write a short program using these APIs to enumerate these directories and delete them.

1
  • Caution: If you use \\?\, then you need to know what type of file system is on the mounted volume. (e.g., you need to know whether '/' or '\' is the separator character.) The WinAPI docs say, For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system. (msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx) May 19, 2015 at 14:44
7

The fix is simple and free! No need to rename the folders at all.

  1. Download Cygwin for free.

  2. use the command rm -f -r <folder-name> and wait a few moments.

I had a folder of size 1.5 GB with numerous inner folders and it worked!

5

A method mentioned in this Windows Sysinternals forum post by maxaha1 worked.

I suggest that you work top down. As long as you stay within the same volume (share), you can usually drag the upper level directories to new parents, without regard for the contents of the directory being dragged (ie. and the length / recursion issues it might have). So you take a directory maybe 4 or 5 levels down your problem path, and drag it to the root. Then delete the original path that now only contains a few levels. Ideally, work to keep the longest path to less than 250 chars (includes filenames). I'm doing a system migration at the moment, and we have some users with 340+ char paths which dont copy without this method. I've tried subst's, recursive net use's, recursive scripts, etc.. Unfortunately, users seem to be able to create things that befuddle the best coders, admins etc..

Create new folders and just drag-and-drop, several folders down from the top each time (and delete them) until you get it all cleaned up.

2
  • FileUtils.deleteDirectory("C:\\My\\Stupid\\Path\\"); If you want to use Apache Commons file utils and run a quick java program.
    – Zoidberg
    Jan 8, 2015 at 22:34
  • An interesting idea and kudos for the official blessing, however this is horribly manual and time consuming, compared to many of the other anwers.
    – Barett
    Aug 8, 2015 at 15:31
4

i'd faced the same problem using eclipse..! a zillion nested folders got created and it took up more than 500MB space!!!!

i tried a ton of things in windows and nothing worked..

finally i just rebooted using ubuntu and deleted that horrible folder.. just like that.. ubuntu doesn't seem to have any such access restrictions based on filepath length as windows does.. makes life a lot easier.. :)

1
  • 2
    +1 I also wanted to emphasize for users not familiar with Linux variants. You don't need an installed copy of Ubuntu to run it for a simple task like this. You can easily download the LiveCD version and boot the OS from a disc. Feb 9, 2011 at 3:36
4

The previous three responses are obviously spam (for a payware demo). If you want a free one-click solution, see here:

http://www.osmstudios.com/Display.asp?Page=pathtoolong

Which probably employs the 3-line Java from further up this thread.

-fm

3
  • I wrote this and that is exactly what it uses. Jun 5, 2012 at 0:08
  • This solution is useful only if you do not have to update the ownership and/or permissions more than 260 characters deep into the nested folders and files.
    – Zarepheth
    Sep 21, 2012 at 23:42
  • 7Zip File manager is free to use. Open file using the 7Zip File manager and Shift+Delete your file. My comment is 1st on the previous 3 responses.
    – Swanidhi
    May 14, 2015 at 12:45
3

Programmatically you can delete using DeleteFileW and RemoveDirectoryW, and prepending L"\\?\" to the file names. Note that you have to use the W version not the A version of the APIs.

1
3

Someone above already posted a reference to "rimraf", but let me add more details. If you have Node installed on your system, then simply use the rimraf module (it can be installed on your Path). This is by far the easiest way to delete things on a Windows system in the most non-trivial of cases. It isn't so much that rimraf is awesome, actually, it's because Node.js handles filesystems at the device kernel level. If you look at the 'fs' module source code for Node, you'll see code such as this. In essence, Node.js handles filesystems much better than even Git for Windows does i.e. there is a bug using git clean -fdx on Windows that doesn't delete folders with long pathnames.

2

MIR (mirror a complete directory tree )option worked for me in windows and window network paths as well..

robocopy empty_dir <foldertobedeleted> /MIR

  • Just create an empty directory, and tell robocopy to purge everything in the target directory which isn't in the empty directory

output would be like this..

enter image description here>

1
  • 1
    simple yet complete answer
    – Sean Ch
    Mar 16, 2020 at 15:41
1

Lol I had a similar problem a few years ago. Due to a bug I created indefinite nesting of folders until the filename got too long.

IIRC, I deleted them programmatically by crawling through the folder structure.

1

Okay I tried searching for a solution for this and I found one of the easiest solution for windows users

  1. Install Mobaxterm
  2. Open mobaxterm. Go to the directory you wish to delete(using cd) and run the following command

    rm -rf directoryname

All the folder, sub folders which windows was complaining about deleting will now be deleted

1

I wasn't really happy with any of the solutions posted here and in other places (robocopy, 7zip, subst, shady spyware) so I've rolled my own small command line tool.

It can delete a file or an entire directory recursively. Maybe in the future I'll add more options.

The tool is called SuperDelete and you can get it from the releases page page GitHub.

0

Its quite annoying that in 2016 this still presents us Windows users a problem when Linux solved this years ago. It's made even more annoying when its do easy to do in Java.

We should use the Change.org to petition MS to include this ability. It can't be hard. If they are worried about backwards compatibility why not just have a Strict mode that admins can turn on or off. At least give us the warning but then allow us to delete it anyway. GRRRRR [/rant]

If you are running Java on your machine you might find this code helpful:

import java.io.File;

public class Delong {

    public static void main(String[] args) {
        File file = new File("F:\\YourDirectory\\YourFile");
        boolean result = deleteDirectory(file);

        if(result)
            System.out.println("Deleted");
        else
            System.out.println("Failed");
    }//end main

    /**
     * Force deletion of directory
     * @param path The path you want to delete
     * @return True if it worked;
     */
    static public boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int x = 0; x < files.length; x++) {
                if (files[x].isDirectory()) 
                    deleteDirectory(files[x]);
                 else 
                    files[x].delete();
            }//end for
        }///end if
        return (path.delete());
    }//endDeleteDirectory

}//end class
0

I came across a similar problem and using 7-zip(a third-party software) helped me.

  • Install 7-zip https://www.7-zip.org/
  • Open "7-zip File Manager"
  • Using it, navigate to your folder and change the name of the file/folder you want to delete into a simple one.
  • Delete the file/folder with Shift + Del (which might save you from facing the same problem at the Recycle Bin, which might fail again)
-1

You can use "Cygwin" with the command "rm --force --recursive PATH" or short "rm -fr PATH".

1
  • 2
    downvoted because this is a duplicate answer (search for "Cygwin" on the page)
    – user
    Jul 19, 2015 at 12:35
-1

If you already have git installed on your system, then a straightforward way of removing the folder with all its contents, is to open a gitbash window, and then run this command.

$ rm -rf DeleteFolderName

0
-2

Try using Unlocker. It is used for killing processes holding locks on files, but I think it can force-delete f-ed up files, like you have.

2
  • 1
    But this tool just removes exclusive locks on files. It can't help if the file path is too long.
    – Tomalak
    Feb 15, 2009 at 15:58
  • If using 7Zip, be aware that you need to SHIFT-DELETE Oct 22, 2018 at 9:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.