How to delete a file in Windows with a too long filename?
Solution 1
When you want to completely delete a directory and it contains long paths, robocopy
does a VERY good job:
mkdir empty_dir
robocopy empty_dir the_dir_to_delete /mir
rmdir empty_dir
rmdir the_dir_to_delete
This works because robocopy internally uses the Unicode-aware versions of Win32 functions, with the \\?\
prefix for file paths; those functions have a limit of 2¹⁶-1 (32,767) characters instead of 259.
You may need to go through this process more than once to get rid of all of the files.
Solution 2
From a command prompt:
dir /X
This will list your files/folders in short name format. Then use the short name exactly as written to delete the file:
del LONGF~1.txt
Solution 3
I progressivley work my way into the path, renaming each successive parent folder to "1" and attempting to delete. You're effectively shortening the path each time and I've never had to work in by more than 4 or 5 directories until I'm finally able to delete the entire directory structure (which may or may not be what you want). You could do this from the last child folder as well and work your way up or down.
Solution 4
A trick I have used to get round the "full path and filename" length limitation in order to move, copy or delete something is to shorten it by 'breaking in' halfway down (or more) using a mapped drive letter pointing to a folder way down the path.
so you have c:\some\long\path...\and\foo\bar\folders\oldfiles\myoldfile.txt.
Then map an arbitrary drive letter to somewhere along the path so that the first chunk of the path becomes only a few characters long. Pre-requisite - the folder must be in a shared folder (which it may already be if it is on a server, which is where I have needed to do this), and if it is not already then pick a folder somewhere in the path and share it. Depending on your environment and paranoia level, allow everyone modify access to the share as long as the NTFS permissions are reasonably restrictive. If you want, just allow modify rights only to your own account.
Now go to the shared folder or one inside it and share it, or use the command line as follows. Assume you shared folder "foo" as "fooshare", then you could do
net use x: \\mycomputername\fooshare\bar\folders /persistent:no
and the X: drive now points directly to the folder "folders" inside that share, so "x:\oldfiles\myoldfile.txt" is now pretty short.
(The "/persistent:no" means this won't survive the next reboot and confuse you later on. Don't forget to un-share your folder when done.)
Remember, you don't have to share the folder containing the file necessarily, if it is already inside a shared folder you can just map through the share and the nested folders to a target folder near to the file and that works fine.
I've had to use this technique doing a massive robocopy between two servers when we realised that users had mapped drives quite deep in the folder structure, so they had been able to use 255 characters from there, but that exceeded the total file path length when accessed from the local drive root.
Solution 5
In some programs, including Command Prompt (cmd.exe
), you can get around the file length limit by prefixing the full path with \\.\
like this:
\\.\C:\some directory\other directory\a file with long name
Related videos on Youtube
user3048
Updated on September 17, 2022Comments
-
user3048 over 1 year
My wife has several files and folders that somehow ended up having filenames that have caused them to be undeleteable (can't be deleted) by normal means or via the command line. I believe the filenames are too long due to the depth of the folder structures. Does anyone know of a good utility for cleaning up files like this?
-
Nick about 13 yearsHow were these files created?
-
Stefanos Kalantzis over 10 yearsSorry for my ignorance on this topic, but shouldn't Windows handle these files? Shouldn't what Will Eddins posted be done automatically by Windows (even from explorer) ?
-
AStopher over 8 years@Mokubai- The duplicate question should be marked a duplicate of this one, as this question is older.
-
Ellesedil about 8 years@cybermonkey: And it has a better answer.
-
Christian Gollhardt over 7 yearsFor further readers, the 7zip Method with
CTRL
+DELETE
is the easiest method in my opinion... -
Andrew over 6 years
-
dutoitns about 4 years@christian-gollhardt 7-zip, select the file and click SHIFT + DELETE. CTRL + DELETE didn't work for me. SHIFT + DELETE in Windows deletes files without moving them to the Recycle Bin. 7-zip is great, I once had issues with zipping something using the default Windows zip implementation due to too long files names but everything worked 100% using 7-zip. Agreed - also the easiest method in my opinion. I wish this question wasn't closed so that the 7-zip solution could be more visible in the form of an answer.
-
-
Petra over 14 yearsI like that one, it's a nice bit of lateral thinking.
-
Will Eddins over 14 yearsWhile I can't guarantee it will work in this case, I've used it several times to delete folders that have invalid characters at the end that make them impossible to delete by normal means.
-
Ivo Flipse over 14 yearsIt's funny how often an Ubuntu Live CD will help troubleshooting Windows problems ^^
-
Petra over 14 yearsI've noticed that myself, dodgy network try a live CD, filesystem issue try a live CD, corrupt partition table etc. etc. :-)
-
Charles Roper over 14 yearsI just used this technique to delete a file with an invalid filename - it was a file named "},". This tip solved it. Thanks Guard.
-
Andrew Arnott over 13 yearsThis was the only thing that worked for me. All the other tricks given here and in other forums such as this didn't work.
-
Bobson over 12 yearsThis works for files and folders in the current directory, but if you somehow manage to find yourself inside a folder whose path is too long, it will not help. For example, I am currently in a console in a too-long path and cannot even
dir
orcd ..
. -
Antoine RODRIGUEZ almost 11 yearsEfficient when there is no shortname (8.3) stored in the filesystem.
-
BadHorsie over 10 years@Bobson If you cannot
dir
usepushd
instead. That worked for me. -
Ciantic over 10 yearsI had a 8.3 filenames, but they didn't work. This did work.
-
Samir about 10 yearsThis worked nicely with my stubborn Windows Store cache files that refused to be deleted. Thanks!
-
actionshrimp almost 10 yearsI had to add /purge to the robocopy line for this to work, but it did the trick after that
-
Benoit almost 10 years@actionshrimp,
/MIR
implies/PURGE
. I don't understand how adding/PURGE
could alter the behaviour! -
Jay Michaud almost 10 yearsRobocopy is what got me into this mess, but it never occurred to me to use Robocopy to get me out of it. Great answer! Thanks!
-
David Frye over 9 yearsThis is the only solution that worked for me. I love you, Linux! <3
-
Nestor Ledon over 9 yearsWas the only suggestion that worked oddly enough.
-
mihi over 9 yearsyou can avoid the sharing by using
subst x: C:\Some\first\part\of\the\long\path
and afterwards delete the drive withsubst x: /d
-
nickb about 9 yearsThis works like a charm. Dig down there (for me, it was super-nested
node_modules
folders), drag it out to your desktop and delete away. Rinse and repeat as you go up a few folders at a time. What an obnoxious problem. -
Alexander Varwijk almost 9 yearsThis worked for me, a shortcut that helped me was
mv * 1 && cd 1
. This didn't work when multiple files were in the directory but at that point anrm -rf *
usually did the trick. -
SarahofGaia over 8 years@Benoit: 2³²??? Holy crap that's a high number! >_<
-
SarahofGaia over 8 years@bgallagh3r: That's a lot of internets. Specifically, you are saying they win 10⁴ × 59.9 EiB = 599 000 EiB. That's 585 ZiB, or 691 ZB!!! You sure they're worthy of that much data?! >_<
-
SarahofGaia over 8 years@Benoit: I don't understand command line to this level of a degree, yet. For the example you gave, do I just type in what you have typed there? And if so, what do I replace with what?
-
Scott - Слава Україні over 8 years@Alexander: Arguably, your suggestion is off-topic, because this question is tagged [windows]. Unless you're suggesting that it might be necessary even with a Linux live CD. Or you're suggesting Cygwin, as was done here. Now that this question is closed as a duplicate, you might want to re-post your comment on the other one.
-
Alexander Varwijk over 8 yearsI thought I somehow made the above work on windows (without any additional tools). However, the way I wrote it down of course doesn't work. Sadly I can't reproduce it at this time :/ Maybe I just missed the Windows tag.
-
Julian Knight over 8 yearsNice idea but didn't work for me with 10's k's of recursive folders. Only rimraf worked.
-
Julian Knight over 8 yearsNice try but when you have 10's of k's of folders nested it isn't possible.
-
Julian Knight over 8 yearsThe free rimraf works just fine and worked when all the other ideas failed. It requires node.js
-
Bobby Borszich over 8 yearsThe ability to fix this quickly without additional software was a big win in my book! nice job
-
Benoit over 8 years@SarahofGaia, my bad, it's 2¹⁶ - 1 actually
-
Manoj Attal over 8 yearsSaved by day...
-
UpTheCreek over 8 yearsThis didn't work for me - the paste operation failed because of the long filename - doesn't matter where cut it.
-
aliopi about 8 yearsamazing answer, it feels like old and for mere mortals forbidden windows power
-
Steve Chambers about 8 yearsGreat answer! Much better than the ones that tell you to rename by hand or delete files individually - with this you can just kick it off, come back in a while and it's all done. Could just be worth adding to the answer that robocopy comes with recent versions of Windows (Vista+) - I wasn't familiar with it and assume some others might not be either.
-
Avalanchis about 8 years@Benoit: According to the robocopy command line help, /MIR is equivalent to /E plus /PURGE, so wouldn't this mean that /S is not necessary? (/E is the same as /S but includes empty subdirectories)
-
Dawesi almost 8 yearsmain example didn't work on windows 10... didn't copy any of the files from the dir...] I'm thinking unicode was the issue in the first place... most US devs don't account for the majority of people in the world not using english.
-
Dawesi almost 8 yearsCan't do this in windows 10... rename throws "filename to long' error
-
Dawesi almost 8 yearsdoesn't work in windows 10
-
Dawesi almost 8 yearsRun bash from windows No Linux required. ;-) Also if you're running windows 10 just install “Windows Subsystem for Linux" basically windows uses ubuntu api hooks to get the job done... utils including "apt, ssh, rsync, find, grep, awk, sed, sort, xargs, md5sum, gpg, curl, wget, apache, mysql, python, perl, ruby, php, gcc, tar, vim, emacs, diff, patch, and most of the tens of thousands binary packages in the Ubuntu archives!" This is a very full Linux development environment that just happens to be running on Windows. zdnet.com/article/ubuntu-and-bash-arrive-on-windows-10
-
Stephen Chung almost 8 yearsThe
subst
trick seems to work well, as long as the filename is not so long as to making it go over 260 characters even at the root of a drive. -
eomeroff almost 8 yearsWorks great but slow.
-
Damon over 7 yearsWorked for my purposes in window 7. Had to rename over 7 folders deep.
-
eselk over 7 yearsNot only did this fix the problem for me, it also explains how I ended up with the issue in the first place. I must have had a path that was near the limit, then I renamed a parent folder (added something like "backup Nov 2016 save" to the name) which pushed the files in subfolders over the limit. Good to know the cause as well as the solution, even though I know it can happen other ways too, I think this is a common way it happens to folks.
-
Philip Pryde over 7 yearsthanks, cmd worked a charm with this in Win7
rm -rf \\directoryname
-
Paulson Peter over 7 yearsThis works great. I am a javascript/angular developer. The modules installed through npm always causes the same problems and i am using this command to delete those modules, I have this answer bookmarked on my browser. @Benoit Thanks a lot...
-
atom88 over 7 yearscygwin is another alternative for running LINUX commands in windows
-
fedmich about 7 yearsWorked for me. I'm on Windows 7 btw
-
kapex about 7 yearsYou also don't need to explicitly share any folder with
net use
, you can use the default admin shares:net use x: \\localhost\c$\bar\folders /persistent:no
-
Monis about 7 yearsperfect answer!
-
Loenix almost 7 yearsThis does not work with Windows 10, the displayed file name is the long one
-
Martin Argerami almost 7 yearsThis doesn't work when the problem is the whole path and not just the filename. Robocopy cannot delete the files in that situation.
-
James Wilkins almost 7 yearsThe only one that worked for me in Windows 10, thanks! ;) Such a stupid thing really. I can't believe MS would allow files to be created that people can't delete. Just add to my list of stupid things Windows (MS) does.
-
MKANET over 5 yearsThis worked when all other methods didn't work such as (dir /x, using System user account, moving file, renaming file, etc)
-
Ruslan López almost 3 yearsin a non-server computer it takes about 20-30 minutes. +1