Having issues deleting a folder or a file? Are you getting messages about filename too long or path too long? Then read on for a simple solution.
Windows uses a naming convention called “Long Filenames (LFN)”. The LFN system supports file names up to 255 characters. Windows also has a flag called MAX_PATH which has value 260, meaning a file path can have a maximum of 260 characters including the drive. So, when files are created by other programs that have more than 260 characters they cannot be deleted using Windows or DOS commands. You’ll get the message file name too long or path too long.
Method 1: Using 7zip
7-Zip is open source file archiver with a high compression ratio.
- Find the path of the file with the filename that is too long to delete
- Navigate to the file, as if you’re going to create an archive within the 7-zip or winrar application, right click on the file (or F2) and choose to rename. Give the file a shorter name.
- Success! the file now has a shorter name and can be deleted.
Method 2: Using Robocopy
Robocopy is default available on Windows 7 and higher. Robocopy is a Windows command to copy files from one location to another.
The syntax for Robocopy is quite easy:
robocopy <Source> <Destination> [<File>[ …]] [<Options>]
There is an option called PURGE. The PURGE option deletes destination files and directories that no longer exist in the source. We will use this flag to delete the folders recursively.
- create an empty directory (C:\emptyfolder). Let’s say the folder we want to delete is C:\deletefolder
- Run robocopy c:\emptyfolder c:\deletefolder /purge. Since there are no files or folders in the source directory (C:\emptyfolder), it simply deletes the files and folders under the target directory (C:\deletefolder) recursively!
Method 3 (For NodeJS users): Using RimRaf
Working with Node.js on Windows platform, can be tricky and it might happen that some trivial action will fail, for instance removing nodemodules folder. This is due to recursive references of folders and files within nodemodules, which ultimately ends up with a long name (path).
- Install RimRaf npm install rimraf -g
- Delete the folder using rimraf nodemodules