-1

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.

4
  • 1
    Open 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". Commented May 29, 2022 at 0:29
  • @Compo What's the difference between del and rd? Commented May 29, 2022 at 0:49
  • del or erase deletes files. rd or rmdir deletes directories. Commented May 29, 2022 at 1:56
  • Does this answer your question? Delete files or folder recursively on Windows CMD Commented May 29, 2022 at 16:43

2 Answers 2

-1

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]
  1. "/s" is to delete it recursively
  2. "/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.
  3. You must not write the square brackets I wrote in the command example. Just the folder name, as it is.
Sign up to request clarification or add additional context in comments.

6 Comments

No you don't, you just need to use the RD command properly! Perhaps you should follow my advice under the question itself.
@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 , 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.
@Dedec0 - Just edit this answer; no reason to have two answers
@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.
|
-2

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.

8 Comments

I 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.
Please read my description about the commands DEL and RD in my answer on Delete files or folder recursively on Windows CMD. 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.
Summary: 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.
@Dedec0 Your rollback is destructive. Rants like that have no place in questions or answers on Stack Overflow.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.