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.
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.
6 Comments
RD command properly! Perhaps you should follow my advice under the question itself.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
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./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.
rd /?, orrmdir /?, and press the[ENTER]key. The needed options are clearly defined within that output.RD /S /Q "S:\ome\Directory".delandrd?delorerasedeletes files.rdorrmdirdeletes directories.