I need a Windows command to delete a directory and all its containing files but I do not want to see any errors if the directory does not exist.
5 Answers
Redirect the output of the del
command to nul. Note the 2
, to indicate error output should be redirected. See also this question, and especially the tech doc Using command redirection operators.
del {whateveroptions} 2>null
Or you can check for file existence before calling del
:
if exist c:\folder\file del c:\folder\file
Note that you can use if exist c:\folder\
(with the trailing \
) to check if c:\folder
is indeed a folder and not a file.
-
1Tried this i still get "The system cannot find the path specified" – jaywayco Jan 24 '13 at 13:38
-
5Force recursive deletion, ignore errors: `rmdir /s /q some\where\myFolder 2>nul` – crusy Jan 23 '19 at 14:29
-
@crusy Much appreciated, but that is the same answer (`rmdir` = `rd`) that dbenham already gave exactly _6 years_ ago – GolezTrol Jan 24 '19 at 12:11
Either redirect stderr to nul
rd /q /s "c:\yourFolder" 2>nul
Or verify that folder exists before deleting. Note that the trailing \
is critical in the IF condition.
if exist "c:\yourFolder\" rd /q /s "c:\yourFolder"

- 127,446
- 28
- 251
- 390
-
18@GolezTrol - Neither of you deleted the folder as requested by the OP. Both of you focused on deleting a file. – dbenham Jan 24 '13 at 15:38
For me on Windows 10 the following is working great:
if exist <path> rmdir <path> /q /s
q
stands for "delete without asking" and s
stands for "delete all subfolders and files in it".
And you can also concatinate the command:
(if exist <path> rmdir <path> /q /s) && <some other command that executes after deleting>

- 1,447
- 3
- 20
- 36
-
1Concatinate helps me, otherwise it wont to work when `rmdir` paired with `if exist`. – noszone Jul 23 '21 at 11:37
You can redirect stderr to nul
del filethatdoesntexist.txt 2>nul

- 30,582
- 35
- 123
- 152
-
2Did you try this? This doesn't work. If it doesn't exist, I still see the error – dgo Jul 27 '17 at 00:42
The above comes up with Y or N in the prompt. So, I used the following instead and it works perfectly.
if exist cddd rmdir cddd
Hope this helps someone.
Cheers.

- 8,353
- 4
- 51
- 54
-
To avoid the prompt you have to add __/Q__ to the rmdir command. If you need to remove all files and subdirectories you need also __/S__. You didn't see the prompt probably because the directory was not there. – Bemipefe Jan 21 '20 at 13:12