I need to delete one folder containing other folders and files inside. I tried del and rmdir commands but sometimes they fail with some error lines: [PATH]: The directory isn't empty.
Is there any good alternative?
18 Answers
This happens to me a lot with my automated build scripts.
I guess the reason might be some application that has a file open in that directory with "share delete". I.e. the application allows a deletion of the file (which is why I figure the DeleteFile call doesn't fail), but the file will only disappear after said application has closed it's handle.
That means the file might still be there when the rmdir command tries to delete the folder, hence the error message. Soon after that, said application will close it's handle, the file will disappear, and when you inspect the folder to see which file rmdir was talking about it will be empty.
At least that's my theory.
The workaround proposed by Harry Johnston looks good. Only I would insert a pause in between the rmdir commands. Of course Windows has no easily scriptable "pause" command (correction: ancient Windows versions don't, newer have - see comments). But if seconds granularity is enough one can use ping to create a pause:
ping -n {desired_delay_in_seconds + 1} 127.0.0.1 >nulSo in total:
rd /s /q foo
:: retry once
if exist foo ( :: clear errorlevel cmd /c :: pause ping -n 2 127.0.0.1 >nul :: retry rd /s /q foo
)
:: retry yet again
if exist foo ( cmd /c ping -n 2 127.0.0.1 >nul rd /s /q foo
)
:: give up
if exist foo {panic} 6 Try:
rmdir /S your_directoryor:
rmdir /S /Q your_directory to skip confirmation messages.
6You may have some readonly files, you can use the del /F option to get rid of them using
del /S /F your_directory rmdir your_directoryYou could also have some hidden files and if you are really sure you want to delete them, then you can do this using
del /S /F /AH your_directory rmdir your_directoryIf this still fails, then either you do not have permission to delete some files, or some of the files are still in use.
1I believe there's a bug in Windows 7 (and perhaps other versions) which sometimes causes this symptom; or it might be a bug in third-party software. (Do you have Symantec Endpoint Protection installed by any chance?)
Anyway, I've run across it fairly often. In most cases, the problem can be worked around by running rd /s /q two or three times in a row.
If this is in a batch file, you can do something like this:
rd /s /q foo
if exist foo rd /s /q foo
if exist foo rd /s /q foo
if exist foo echo Help! & pause 1 Use del on the files inside, then rmdir to remove the folder.
To use the rmdir method to remove all the files as well, use the /S switch before the directory name, and /Q to suppress prompting for deleting. This is the best way to do it, as you don't miss any files whatsoever. Be careful using the /Q switch though, as it will not warn you of System or Hidden file attributes
If you use node you can use the rimraf dependency like this:
run install: npm install rimraf -g
delete folder: rimraf SourceFolder
This helped me when getting the error:
2the source file names are larger than is supported by the file system
I think you can use it like this:
msg*your file is going to delete
pause
del/s /q "C:\Users\Rd\Desktop\New folder (2)\"
rmdir /s /q "C:\Users\Rd\Desktop\New folder (2)\"
mkdir "C:\Users\Rd\Desktop\New folder (2)" 2 Folder older versions of Windows (DOS, Windows 95/98/ME), DELTREE is the equivalent to RM or RMDIR. I use DELTREE on my Windows 7 workstation in batch files just fine though.
Deletes a directory and all the subdirectories and files in it.
To delete one or more files and directories:
DELTREE [/Y] [drive:]path [[drive:]path[...]] /Y Suppresses prompting to confirm you want to delete the subdirectory. [drive:]path Specifies the name of the directory you want to delete.
Note: Use DELTREE cautiously. Every file and subdirectory within the
specified directory will be deleted.