DEV Community

Cover image for Windows RMDIR Command: Delete a Folder and Everything Inside It
arnostorg
arnostorg

Posted on

Windows RMDIR Command: Delete a Folder and Everything Inside It

You try to delete an old build folder from CMD and get this:

C:\>rmdir C:\build\old
The directory is not empty.
Enter fullscreen mode Exit fullscreen mode

rmdir is careful by default. It only removes empty folders. To delete a folder together with every file and subfolder inside it, you need two switches, and you should know exactly what they do before you type them, because CMD has no Recycle Bin and no undo.

This post covers the version I actually use: empty vs. full folders, /s and /q, paths with spaces, clearing a folder but keeping it, and the batch-script guard that stops rd from going after the wrong drive.

RD and RMDIR are the same command

rd is just the short name. Microsoft documents them as the same command, so everything below works with either spelling:

rmdir C:\build\old
rd C:\build\old
Enter fullscreen mode Exit fullscreen mode

Syntax, straight from the docs:

rmdir [<drive>:]<path> [/s [/q]]
Enter fullscreen mode Exit fullscreen mode

Delete an empty folder

With no switches, rd deletes the folder only if it is empty:

rd C:\build\empty-folder
Enter fullscreen mode Exit fullscreen mode

That's the safe default. If anything is inside, including hidden or system files you can't see in a normal dir, you get The directory is not empty. and nothing is deleted.

To see what's blocking it, list everything, hidden files included:

dir /a C:\build\old
Enter fullscreen mode Exit fullscreen mode

Delete a folder and everything inside it: /s and /q

rd /s C:\build\old
Enter fullscreen mode Exit fullscreen mode
  • /s deletes the whole directory tree: the folder, all its subfolders, and all files.
  • Without /q, rd /s asks for confirmation before deleting the tree. Answer Y to go ahead, N to cancel.

For scripts, where nobody is there to answer, add /q:

rd /s /q C:\build\old
Enter fullscreen mode Exit fullscreen mode
  • /q is quiet mode: no confirmation prompt.
  • /q only works together with /s. On its own it does nothing useful.

Microsoft's own docs put a caution box on this one: in quiet mode the entire tree is deleted without confirmation. Treat rd /s /q like a loaded command and always know what the path resolves to.

Want to try the folder commands without risking a real disk? You can practice CMD folder commands like rd, mkdir and dir in a free browser terminal and break things on purpose.

Preview before you delete

Before running rd /s /q on anything that matters, look at what's in there:

:: every file and folder, full paths, one per line
dir /s /b C:\build\old

:: the same, drawn as a tree with the files
tree /f C:\build\old
Enter fullscreen mode Exit fullscreen mode

If that list is longer than you expected, stop and check the path. Ten seconds of reading beats restoring from backup.

Paths with spaces need quotes

CMD splits arguments on spaces. Without quotes, this does not mean what it looks like:

rd /s /q C:\Temp\Old Builds
Enter fullscreen mode Exit fullscreen mode

rd sees two paths: C:\Temp\Old and Builds (relative to the current folder). Best case, you get errors. Worst case, a folder called Builds in your current directory really exists and is gone.

Always quote paths:

rd /s /q "C:\Temp\Old Builds"
Enter fullscreen mode Exit fullscreen mode

Delete the contents but keep the folder

Sometimes the folder itself has to stay (a share, a path an app expects, a folder with custom permissions) and only the stuff inside should go.

Option 1: delete and recreate. Simple and fine for scratch folders:

rd /s /q "C:\build\output"
if not exist "C:\build\output" mkdir "C:\build\output"
Enter fullscreen mode Exit fullscreen mode

Keep in mind the recreated folder is a new folder: custom permissions or attributes set on the old one are not carried over.

Option 2: delete only what's inside. Files first, then each subfolder:

del /q "C:\build\output\*"
for /d %i in ("C:\build\output\*") do rd /s /q "%i"
Enter fullscreen mode Exit fullscreen mode
  • del /q removes the files directly inside the folder without prompting.
  • for /d loops over the subfolders, and rd /s /q removes each one with everything in it.

In a .bat or .cmd file you must double the percent signs on the loop variable:

for /d %%i in ("C:\build\output\*") do rd /s /q "%%i"
Enter fullscreen mode Exit fullscreen mode

At the prompt it's %i, in a batch file it's %%i. Mixing those up is the most common reason this line "does nothing".

Hidden files and folders need extra care here; check with dir /a afterwards and see the del and attrib docs if something survived.

The batch-script trap: an empty variable

This is the one that actually causes outages. A cleanup script like this looks harmless:

rd /s /q "%TARGET%\"
Enter fullscreen mode Exit fullscreen mode

Now imagine TARGET was never set, because of a typo, a missing argument, or a different user context. The line becomes:

rd /s /q "\"
Enter fullscreen mode Exit fullscreen mode

A path that starts with a backslash is resolved from the root of the current drive. So instead of deleting one folder, rd is now pointed at the top of the drive. Best case, you get a wall of access-denied errors. Worst case, it quietly deletes everything the account has permission to delete.

Guard every rd /s /q that uses a variable:

@echo off
setlocal

if not defined TARGET (
  echo TARGET is not set. Refusing to delete anything.
  exit /b 1
)

if not exist "%TARGET%\" (
  echo "%TARGET%" does not exist. Nothing to delete.
  exit /b 0
)

rd /s /q "%TARGET%"

if exist "%TARGET%\" (
  echo Some items in "%TARGET%" could not be deleted.
  exit /b 1
)

echo Deleted "%TARGET%".
Enter fullscreen mode Exit fullscreen mode

What each part does:

  • if not defined TARGET stops the script when the variable is missing.
  • if not exist "%TARGET%\" (with the trailing backslash) checks that the path exists as a folder.
  • The final if exist checks the result. Don't rely on %errorlevel% alone after rd; checking whether the folder is still there is the reliable test.

The same rule applies to %1 and other script arguments: never feed them straight into rd /s /q.

When rd can't delete everything

"Access is denied" or "being used by another process." A file is open in some program (an editor, a running service, an antivirus scan) or your account lacks permission. rd /s /q deletes what it can and leaves the rest. Close the program or stop the service, then run it again. For permission problems, run CMD as administrator only if you are sure about the path.

You're standing in the folder. You can't delete the current directory. Microsoft's docs show the error you get as "The process can't access the file because it is being used by another process." Move out first:

cd /d C:\
rd /s /q "C:\build\old"
Enter fullscreen mode Exit fullscreen mode

Note the parent folder isn't enough if your prompt is inside a subfolder of what you're deleting. Change to a folder outside the tree.

Very long paths. Classic Windows APIs limit a path to MAX_PATH, 260 characters. Deeply nested folders (looking at you, node_modules) can end up longer than that, and rd then fails on them. A widely used workaround is to mirror an empty folder over the problem folder with Robocopy, which empties it, then remove the leftovers:

mkdir C:\empty
robocopy C:\empty "C:\build\deep-folder" /mir
rd /s /q "C:\build\deep-folder"
rd C:\empty
Enter fullscreen mode Exit fullscreen mode

/mir deletes everything in the destination that isn't in the source, and the source is empty, so be very sure about the destination path. I covered /mir and its risks in Windows Robocopy Command: Mirror a Folder Without Deleting the Wrong Files.

The PowerShell equivalent (with a real dry run)

rd has no preview mode. PowerShell's Remove-Item does:

Remove-Item -Path 'C:\build\old' -Recurse -Force -WhatIf
Enter fullscreen mode Exit fullscreen mode

-WhatIf shows what would be deleted without deleting anything. Drop it when the list looks right. I wrote a full post on this: PowerShell Remove-Item: Dry-Run Deletes with -WhatIf Before You Nuke Folders.

Cheat sheet

Goal Command
Delete an empty folder rd "C:\path"
Delete a folder and everything inside (asks first) rd /s "C:\path"
Same, no prompt (scripts) rd /s /q "C:\path"
Preview what's inside dir /s /b "C:\path" or tree /f "C:\path"
See hidden files blocking delete dir /a "C:\path"
Empty a folder, keep it del /q "C:\path\*" + for /d %i in ("C:\path\*") do rd /s /q "%i"
Guard a variable in a .bat if not defined TARGET exit /b 1
Dry run in PowerShell Remove-Item 'C:\path' -Recurse -Force -WhatIf

Related CMD basics: Windows MKDIR Command: Create Folders Fast From CMD and Windows DEL Command: Delete Files Safely From CMD.

Official references on Microsoft Learn: rmdir, del, for, if, dir, tree, attrib, robocopy, Maximum path length limitation, and Remove-Item.

Practice it where mistakes are free

rd /s /q is a command you want in muscle memory, but not one you want to learn on a production server. CMD Master's free interactive Windows command line course gives you a real terminal in the browser with hands-on CMD, PowerShell, and Bash drills and instant feedback, so you can create, list, and delete folder trees as often as you like.

Quick quiz to finish: in a batch file, what does cd /d %WORK% & rd /s /q . do if %WORK% was never set? Write your answer in the comments before you test it.

Top comments (0)