Files or folders can sometimes not be deleted because of file name restrictions in Windows. See, Windows supports file names of up to 255 characters, which includes the directory path. If a directory or file surpasses that limit it cannot be deleted. Many users at this moment assume a corrupt file on the hard drive that is preventing the deletion. While that is a possibility, it may be simpler than that.
The error message reads (variants possible):
cannot delete file: the file name you specified is invalid or too long. Specify a different file name.
The following trick may help in removing the file and the directory structure from the system. Please note that it is not necessary to count the characters used in the file path, if it looks extraordinarily long then it may use indeed more characters than the allowed maximum.
We have already established that deletion does not work. The best option? Renaming. It is still possible to rename directories or the file name, so that the total count drops below the character limit. This can be done both in Windows Explorer, or on the command line. Just select a folder, and rename it to a single character. Repeat the procedure until the file path length has dropped considerably.
Deleting the file and folder structure should then work both in Windows Explorer and the command line. There are other solutions on how to cope with this situation. Another possibility is to map a drive to a folder in the path, to shorten the url structure, or to find a software or operating system that can manipulate those files and folder structures.
Did you ever encounter files that could not be deleted because of their length? What have you done to fix the situation?
You may also ask yourself how those structures appear on your system. It can be that you have downloaded and extracted an archive that included a nested folder structure, or are using a data drive on Windows and another operating system that does not have these restrictions. Whatever the reason, your best bet of deleting, renaming or moving files or folders if you receive the "not valid or too long" error is to shorten its path by renaming folders.Advertisement
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.