What ever happened to deltree, and what's its replacement? [closed]
Asked Answered
B

11

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?

Bloemfontein answered 3/12, 2008 at 21:59 Comment(1)
deltree was introduced in version 5.0 (I still remember the glee of being able to use it) - Man I feel old.Birecree
S
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.

Soto answered 15/1, 2013 at 19:57 Comment(13)
@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.Soto
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.Aleda
@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.Soto
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.Aleda
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.Aleda
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. :-\Soto
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.Aleda
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.Aleda
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.Soto
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.Aleda
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. ¬_¬Soto
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.Aleda
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 rmdirMembrane
D
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:

Dodecagon answered 3/12, 2008 at 22:7 Comment(1)
"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! :)Depopulate
B
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 *
Bienne answered 28/3, 2015 at 0:4 Comment(1)
This is interesting. I'd like to note that learn.microsoft.com/en-us/windows/win32/api/shellapi/… says that "This function has been replaced in Windows Vista by IFileOperation." Don't know that it matters, though!Victimize
I
8
rmdir /s /q directory
Institution answered 3/12, 2008 at 22:2 Comment(0)
M
5

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

powershell -Command "Remove-Item 'PathToMyDirectory\*' -Recurse -Force"
Mota answered 16/2, 2017 at 14:26 Comment(2)
Unfortunately PowerShell is bloated and slow. :-\ I hate it when companies try to force users to "upgrade" to their latest junk. 😒Soto
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!Victimize
D
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
Downfall answered 3/12, 2008 at 22:8 Comment(0)
C
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.

Callean answered 10/1, 2013 at 17:4 Comment(0)
A
1

to delete a directory and all it's contents recursively

rd /s MY_DOOMED_DIR
Amritsar answered 9/6, 2016 at 21:42 Comment(0)
S
0

Use this:

cd (your directory here)
del *.* /f /s /q
Scrawly answered 17/8, 2018 at 2:35 Comment(2)
Could you please add a brief explanation for the flags? That will make this answer even more valuable. Thanks.Lodgings
This has been mentioned in the accepted answer already.Kiva
O
0

Delete all files and subdirectories

cd /d Directory && rd /s /q .\
Osculum answered 1/10, 2018 at 8:18 Comment(2)
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 advantageArequipa
@jeb, actually it's not. It's slightly different, and that slight difference is crucial and makes it superior. 👍😉Soto
V
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)

Victimize answered 3/2, 2023 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.