88

In earlier versions of MS-DOS - I want to say version 7, but I could be wrong - there was a deltree command, which recursively deleted all subdirectories and files from a given path.

deltree no longer exists, but del didn't seem to inherit the ability to delete a tree. del /s deletes files, but not folders.

How to you easily (i.e., in one command) delete a tree from a batch file?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • 5
    deltree was introduced in version 5.0 (I still remember the glee of being able to use it) - Man I feel old. – Mark D Apr 13 '16 at 15:26

11 Answers11

96

As others have mentioned, the rd command has the /s switch to recursively remove sub-directories. You can combine it with the /q switch to forcibly delete a sub-directory (and its contents) without prompting as so

rd /s /q c:\foobar

What everybody is missing is that rd is not an exact replacement for deltree as seemingly (almost) every page returned by Googling for windows deltree would have you believe. The deltree command worked for both directories and files, making it a single convenient, all-purpose deletion command. That is both of the following are valid:

deltree /y c:\foobar
deltree /y c:\baz.txt

However rd (not surprisingly) only works for directories. As such only the first of these commands is valid while the second gives and error and leaves the file un-deleted:

rd /s /q c:\foobar
rd /s /q c:\baz.txt

Further, the del command only works for files, not directories, so only the second command is valid while the first gives an error:

del /f /q c:\foobar
del /f /q c:\baz.txt

There is no built-in way to delete files and directories as could be done with deltree. Using rd and del individually is inconvenient at best because it requires distinguishing whether a file-system object (file-/folder-name) is a file or directory which is not always possible or practical.

You can copy the deltree command from a previous OS, however it will only work on 32-bit versions of Windows since it is a 16-bit DOS command (even in Windows 9x).

Another option is to create a batch-file that calls both del and rd; something like this:

::deltree.bat

@echo off
rd  %* 2> nul
del %* 2> nul

You would call it as so:

deltree.bat /s /q /f c:\foobar
deltree.bat /s /q /f c:\baz.txt

This calls both rd and del, passing in the arguments and redirecting the output to nul to avoid the error that one of them will invariably emit.

You will probably want to customize the behavior to accomodate or simplify parameters or allow error messages, but even so, it is not ideal and not a direct replacement for deltree.

An alternative is to get a third-party tool, though finding one is a real exercise in search-query-crafting.

Synetech
  • 9,643
  • 9
  • 64
  • 96
  • 2
    @TobyAllen, “rm”? Did you mean *rd*? If so, then you need to re-read the help text; it says *Removes all directories and files **in the specified directory** in addition to the directory itself. Used to remove a directory tree.* Like I said, it does not delete files. If you use it as so: `rd /s foobar.txt`, it will prompt you if you are sure, and if you say yes (or use the `/q` switch), then it gives the error `The directory name is invalid.` – Synetech Jul 02 '13 at 13:52
  • You actually can remove everything under a directory (including its subdirectories) with `RMDIR /S`, so to say there's no way to delete both files and folders in one blow is actually incorrect. – vapcguy Jan 24 '17 at 22:52
  • @vapcguy, `rd /s` only applies to the *directory, not the files inside it*. In order to delete the files in it, you have to use `rd` on the directory itself. For example, you can't say `rd c:\target\foobar.txt`, you'd have to use `rd /s c:\target`, but you may not want to delete the directory itself, just its contents. `deltree` could do that, but `rd /s` cannot, `rd /s` also deletes the directory itself. – Synetech Feb 17 '20 at 21:47
  • Notice I said `RMDIR /S`- not `rd /s`. But even with `rd`, if the directory is gone, how are the files not removed? Even if they weren't, the files are effectively orphaned & should be garbage collected by the system. And even if that didn't happen, it certainly does if you re-create the directory-you don't magically get a list of your files, again. Pointers are essentially removed. So are you saying you just never get that space back, because you are never able to delete those files, again, if you use `rd /s`? Not sure I'd believe that-that'd be a terrible bug. Point is to delete files & dir. – vapcguy Feb 19 '20 at 15:50
  • If you create a directory in your "Documents" folder called "test", create a test text document there in Windows, then close the Explorer window, go to a command line, `cd Documents`, `rd /s test`, your directory and your text file will be removed. `rmdir /s test` works, too. You NEVER try to use `rd /s C:\Users\me\Documents\test\test.txt`- that would be absolutely stupid. But you CAN and SHOULD use the command on the directory, just not on the file name. But by removing the directory you can remove the files. – vapcguy Feb 19 '20 at 15:56
  • `RMDIR /S- not rd /s` I'm not sure what you're saying. Is the dash supposed to be part of the switch? Is the switch case-sensitive in Windows 10? (It's not in Windows 7). `But even with rd, if the directory is gone, how are the files not removed?` That's if you remove the parent directory itself (which isn't always what you want), but even then, the `rd` command doesn't actually work on the files directly. `But you CAN and SHOULD use the command on the directory` Again, you don't always want to remove the directory itself, `deltree` would let you remove only its contents, but `rd` doesn't. :-\ – Synetech Feb 20 '20 at 16:11
  • Jeez, man, no-the hyphen was just punctuation. Did you even look at my next response after that? Did I use a hyphen there? No. No, it's not case-sensitive-did that for emphasis. "Parent directory"? No. It's if you remove **THE** directory that's holding the files. You don't have to do `rd /s C:\Documents` to remove something at `C:\Documents\test`. You can just do `rd /s C:\Documents\test` & that removes a document at `C:\Documents\test\test.txt`. This isn't rocket science. Test & see. Yes, there's times you only want to delete a file, `del` for that-but doubt that's what the OP wanted to do. – vapcguy Feb 21 '20 at 18:23
  • `Deltree` isn't for deleting just the contents of folders. It deletes the folders, too. That's why it has `tree` in the name. `Del` is for deleting individual files. – vapcguy Feb 21 '20 at 18:24
  • I didn't say it's *only* for deleting the contents, I said that's something you can do with it which you can't do anymore with built-in commands. – Synetech Feb 22 '20 at 19:16
  • Except you said that `rd` doesn't replace `deltree`. And you're trying to say *"deleting the contents... [is] something you can't do anymore with built-in commands"*. I just demonstrated how that isn't true. `rd /s` and `rmdir /s` and `deltree` are all built-in commands and *they do delete the contents*. Of course that's not the only thing they do - they delete the folders, too. But it seemed like you wanted to say the contents wouldn't be removed by deleting the folder. They are. – vapcguy Feb 24 '20 at 17:45
  • Why is this so difficult? Deleting just the contents *isn't* something that you can do with built-in commands because `deltree` was never ported to Windows and `rd /s` doesn't delete only the contents, it removes the folder itself. I never said the contents wouldn't be removed, I said `rd` doesn't work directly on files. This is one of the reasons I left the SE network. Arguing over nothing. ¬_¬ – Synetech Feb 25 '20 at 22:35
  • No one would be trying to delete *just the contents* of a folder, if they are using a command that **deletes a folder**. That's silly. `deltree` was never a command to delete *just the contents* of a folder without taking out the folder, too & YES-it WAS active on Windows MS-DOS, at least in Win98. They replaced it with `rmdir`. Of course `rd /s` deletes the folder - it literally stands for "remove directory". If you say *rd doesn't work directly on files*, you're going to confuse people. Emphasize *directly*. But even that's unnecessary to point out, since it WILL remove the files. – vapcguy Feb 25 '20 at 23:02
  • No it was not replaced, as rd (rmdir) existed as it does now as an internal shell command, while deltree was an external one. The one thing that is not easy to do now is to delete all contents of, which could be done with deltree but not with ch could be done with deltree but not with rmdir – arana Feb 24 '23 at 18:45
46

It was replaced with the commands: RMDIR or RD

Delete all subdirectories with /S

Use it quietly with the /Q

Example:

RMDIR /S /Q Folder2Delete
RD /S /Q Folder2Delete

Documentation:

Jeremiah
  • 5,386
  • 9
  • 38
  • 45
  • 4
    "RMDIR /S /Q .\" effectively wipes the current directory and everything under it. Yes, it complains about not being able to delete the current directory, which can be useful. Just make sure you're in the correct directory when running it! :) – Mmm Sep 14 '18 at 18:08
10

Feeling nostalgic, I wrote my own deltree.exe. It works with both directories and files, and uses SHFileOperation() for speed.

https://github.com/ai7/toolbox/tree/master/deltree

deltree v1.01 [Mar 27 2015, 16:31:02] (gcc 4.9.1)

Usage: deltree [options] <path> ...

Options:
  -y    yes, suppresses prompting for confirmation
  -s    silent, do not display any progress dialog
  -n    do nothing, simulate the operation
  -f    force, no prompting/silent (for rm compatibility)
  -r    ignored (for rm compatibility)

Delete directories and all the subdirectories and files in it.

It takes wildcards and you can use it like unix rm:

deltree -rf *
raychi
  • 2,423
  • 1
  • 21
  • 12
  • This is interesting. I'd like to note that https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shfileoperationa says that "This function has been replaced in Windows Vista by IFileOperation." Don't know that it matters, though! – Jody Bruchon Feb 03 '23 at 21:53
8
rmdir /s /q directory
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Nowadays, you can use Powershell to do the same task:

powershell -Command "Remove-Item 'PathToMyDirectory\*' -Recurse -Force"
Rosberg Linhares
  • 3,537
  • 1
  • 32
  • 35
  • Unfortunately PowerShell is bloated and slow. :-\ I hate it when companies try to force users to "upgrade" to their latest junk. – Synetech Feb 17 '20 at 21:51
  • I feel like this answer is an accidental advertisement for CMD over PowerShell: `rd /s /q "dir"` is much shorter to type and easier to read. Still nice to have the native answer for PS available though! – Jody Bruchon Feb 03 '23 at 21:54
4
$ help rd
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

    /S      Removes all directories and files in the specified directory
            in addition to the directory itself.  Used to remove a directory
            tree.

    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
2

Actually RMDIR and RD commands in modern Windows operating system merge both the commands RD and Deltree of Win 98 in a single command. It's an internal command that's why you won't find any RD.exe and RMDIR.exe.

By typing this "RD /?" in cmd without double qoutes you'll get exactly what you want.

Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29
1

to delete a directory and all it's contents recursively

rd /s MY_DOOMED_DIR
Gregg
  • 2,444
  • 1
  • 12
  • 21
0

Use this:

cd (your directory here)
del *.* /f /s /q
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 3
    Could you please add a brief explanation for the flags? That will make this answer even more valuable. Thanks. – rsjaffe Aug 17 '18 at 02:44
  • This has been mentioned in the [accepted answer](https://stackoverflow.com/a/14345791/711006) already. – Melebius Aug 17 '18 at 06:12
0

Delete all files and subdirectories

cd /d Directory && rd /s /q .\
  • Your answer seems to be a duplicate of other answers (some are 10 years old). I can't see any more or better explanations or any other advantage – jeb Oct 02 '18 at 06:26
  • 1
    @jeb, actually it's not. It's slightly different, and that slight difference is crucial and makes it superior. – Synetech Feb 17 '20 at 21:53
0

Others have already posted excellent answers. I regularly use rd /s /q in a command prompt with a for loop to delete directories under the current one: for /d %d in (*) do rd /s /q "%d" (where * can be replaced with names instead, e.g. (dir1 dir2 dirX)

Jody Bruchon
  • 885
  • 6
  • 11