I have tried numerous methods of doing this including Remove-Item
, rmdir
, del
, and rd
. I have seen similar questions, but none of the answers have helped me. To be clear, I am not asking for a specific "book, tool, or software library"; I just want a Batchfile command to recursively delete a folder. I'm sorry if I'm doing something wrong; I'm pretty new here.

- 13
- 2
- 6
-
1Open a Command Prompt window, type `rd /?`, or `rmdir /?`, and press the `[ENTER]` key. The needed options are clearly defined within that output. `RD /S /Q "S:\ome\Directory"`. – Compo May 29 '22 at 00:29
-
@Compo What's the difference between ```del``` and ```rd```? – UxuginLinux May 29 '22 at 00:49
-
`del` or `erase` deletes files. `rd` or `rmdir` deletes directories. – Magoo May 29 '22 at 01:56
-
Does this answer your question? [Delete files or folder recursively on Windows CMD](https://stackoverflow.com/questions/12748786/delete-files-or-folder-recursively-on-windows-cmd) – codewario May 29 '22 at 16:43
2 Answers
To delete a non empty folder, you need something called recursion. What we usually read is that we want to "delete a folder recursively", which means to delete a folder and all its contents, including other folder, with their respective contents and folders, and so on.
The command del does it for you. Have you read its help or its documentation? To see that, the command you use in the cmd program is:
del /?
But this does not directly answer your question. What you need to put in your batch file is:
del /s /q [non empty folder name]
- "/s" is to delete it recursively
- "/q" is to delete it without asking for confirmation of each file or folder being deleted. You may want to remove this item, if you want to choose what will be deleted inside the folder.
- You must not write the square brackets I wrote in the command example. Just the folder name, as it is.

- 12
- 1
- 8
-
1No you don't, you just need to use the `RD` command properly! Perhaps you should follow my advice under the question itself. – Compo May 29 '22 at 00:31
-
@Dedec0 Thank you for your help, but the code in your answer only seems to delete the files inside the folder, and not the folder itself. Perhaps my question is unclear in which case I'm sorry. – UxuginLinux May 29 '22 at 00:42
-
@uxuginlinux , I am very sorry if the recursive del command did not delete folders too. Microsoft official documention does not clearly say this. But I just discovered a solution for you. I will put it in a new answer. – Balaco May 29 '22 at 01:11
-
-
@SomethingDark, I decided to keep the first answer because it is about the **del** command. It works for recursively deleting files in a whole tree of directories, but all folders will be kept intact. This is not what @ UxuginLinux asked, but it may be useful for other people having similar problems. – Balaco May 29 '22 at 01:52
-
@Dedec0 `del /s /q` does not delete all files recursive in a folder. It does not delete files with read-only attribute and it ignores files with hidden attribute. So this answer being off-topic for the question which is about deletion of a folder is not completely wrong, but incomplete. Really read my answer on [Delete files or folder recursively on Windows CMD](https://stackoverflow.com/questions/12748786/delete-files-or-folder-recursively-on-windows-cmd) which completely describes how command __DEL__ can be used to really delete all files matching a wildcard pattern like `*` for all files. – Mofi May 30 '22 at 10:50
For the previous answer, I checked the current Microsoft documentation about commands it has, and it does NOT say clearly that the del command will delete only files.
The command you need to recursively delete a folder, and all files OR folders it contains is:
rmdir [name of the folder] /s /q
Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, but they come AFTER the name of the folder! This is what the command documentation shows, as you may read here.
But there are more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has system files or hidden files, the rmdir command will fail. To solve this problem, you need to do more work. To quote the documentation pointed above:
You can't delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:
The directory is not empty
Use the dir /a command to list all files (including hidden and system files). Then use the attrib command with -h to remove hidden file attributes, -s to remove system file attributes, or -h -s to remove both hidden and system file attributes. After the hidden and file attributes have been removed, you can delete the files.
-
1I think, the first sentence "Deletes one or more __files__." is pretty clear. A description of a command always informs on what can be done with a command and never what cannot be done with the command. That is also the reason why the first sentence of the help of command __RD__ respectively __RMDIR__ is "Removes (deletes) a __directory__." which also does not mention that this command cannot be used for deleting files. Please run `rd /?` in a command prompt window and see where the options `/Q` and `/S` are in output help about the syntax. They are before the path argument. – Mofi May 29 '22 at 16:36
-
1Please read my description about the commands __DEL__ and __RD__ in my answer on [Delete files or folder recursively on Windows CMD](https://stackoverflow.com/a/71547603/3074564). It is completely wrong that the command __RD__ does not remove directories containing system or hidden files. That is complete nonsense. The error message you wrote in your answer is always output on using command __RD__ without option `/S` and the directory is not completely empty, i.e. does neither contain a file nor a subdirectory or a reparse point. But __RD__ deletes the directory with using option `/S`. – Mofi May 29 '22 at 16:41
-
1Summary: Most of your answer is simply wrong. Your answer contains lots of fake facts. You should really completely revise your answer and replace all wrong information by correct facts. – Mofi May 29 '22 at 16:43
-
@Dedec0 Your rollback is destructive. Rants like that have no place in questions or answers on Stack Overflow. – codewario May 29 '22 at 19:55
-
@Mofi The answer is not wrong. It says exactly what is said in the official documentation, and the information solved the poster problem, it is shown! So, **rmdir** can recursively remove all data inside any folder, and by "data", it can be all normal files and directories; the system and hidden files must have their status changed first, and I explained this too. – Balaco May 29 '22 at 20:00
-
@Dedec0 "*it does NOT say clearly that the del command will delete only files*" is not correct as the usage help of command __DEL__ as well as the Microsoft documentation describes clearly that this command is for deletion of __files__. "*Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, but they come AFTER the name of the folder!*" is wrong because the usage help shows the optional options __BEFORE__ the name of the folder and they can be used also __BEFORE__ the name of the folder. – Mofi May 30 '22 at 10:39
-
@Dedec0 "*But there are more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has __system__ files or __hidden__ files, the __rmdir__ command will fail.*" is incorrect as the removal of a directory containing system and hidden files works fine on using `/S` which must be always used on directory not being an empty directory. "*You can't delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:*" is not correct as the directory can be deleted with using `/S`. – Mofi May 30 '22 at 10:42
-
@Dedec0 "*Then use the __attrib__ command with __-h__ to remove hidden file attributes, __-s__ to remove system file attributes, or __-h -s__ to remove both hidden and system file attributes.*" That is not necessary for deletion of a directory containing system and hidden files on using option `/S`. It is also not necessary on using command __DEL__ to delete those files as `del /A /F /Q *` deletes all files in a directory including the system, hidden and read-only files. The __system__ attribute does not effect any Windows command, just the __hidden__ and the __read-only__ attributes. – Mofi May 30 '22 at 10:45