🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA RmDir in Excel — Delete a Folder, but Only If It's Empty

|

VBA RmDir in Excel — Delete a Folder, but Only If It's Empty

TL;DRRmDir deletes a folder only when it is empty. Point it at a folder that still holds files or subfolders and it raises error 75 — the exact opposite of Kill, which deletes any file without asking. "Delete this folder" is really "empty it first, then remove it."

Sub RemoveTempFolder()
    RmDir "C:\Temp\Empty"    ' works only if C:\Temp\Empty has nothing inside it
    RmDir "C:\Temp\Reports"  ' error 75 if Reports still contains any file or subfolder
End Sub

RmDir is the mirror image of Kill. Where Kill shreds files with no confirmation and no undo, RmDir is fussy in the opposite direction: it refuses to touch a folder that still contains anything. That refusal is a safety feature — you cannot wipe a full folder by accident — but it means the everyday request "delete this folder and everything in it" is not one statement. It is a small routine, and knowing why is the whole subject.

What you'll learn

  • The mental model — RmDir removes an empty folder only, the opposite of Kill
  • Why a non-empty folder raises error 75, and why that is a safety feature
  • Why RmDir deletes folders, never files — and Kill is folders' opposite
  • How to empty a folder (Kill the files) before RmDir, and how subfolders force recursion
  • When to reach for FileSystemObject.DeleteFolder — one call, whole tree, no undo
  • The guarded delete pattern that will not stall an unattended macro

The mental model: RmDir removes an empty folder only

The key insight is that RmDir deletes the folder entry, not its contents. It will remove a directory only when there is nothing left inside it — no files, no subfolders. Line this up against Kill and the two form a clean pair:

  • Kill deletes files, any file, immediately, with no Recycle Bin.
  • RmDir deletes folders, but only empty ones, refusing anything with contents.

So VBA's two delete statements are deliberately narrow: one only files, one only empty folders. Neither does the thing people actually type into a search box — "delete a folder and everything in it" — because that is the dangerous operation, and the built-ins make you assemble it yourself.

Error 75: RmDir refuses a non-empty folder

Aim RmDir at a folder that still contains files or subfolders and it stops:

RmDir "C:\Temp\Reports"    ' error 75 — "Path/File access error" if Reports isn't empty

This is the defining behaviour, and it is intentional. RmDir will not let a single line silently destroy a folder full of work. Compare it with its counterpart to feel the asymmetry: Kill "C:\Temp\*.xlsx" erases every matching file with no warning, while RmDir "C:\Temp" on the same folder refuses outright. One statement is all danger; the other is all caution. To remove a populated folder you must empty it first.

Empty it first: Kill the files, then RmDir

The two-step pattern is to delete the folder's files with Kill and then remove the now-empty folder with RmDir:

Kill "C:\Temp\Reports\*.*"     ' delete every file in the folder (permanent — no undo)
RmDir "C:\Temp\Reports"        ' now the folder is empty, so this succeeds

That works when the folder holds only files. If it also contains subfolders, Kill cannot help — Kill deletes files, never folders — and each subfolder must itself be emptied and removed. A folder tree therefore requires recursion: walk to the deepest level, Kill its files, RmDir it, and work back up. Because Kill on *.* is permanent, do this only when you are certain the contents are disposable, and log what you are about to delete before you delete it.

FileSystemObject.DeleteFolder: one call, whole tree, no undo

When you genuinely want "folder and everything in it, gone," the FileSystemObject has the single call that RmDir refuses to be:

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")     ' late binding — runs on any machine
fso.DeleteFolder "C:\Temp\Reports"                        ' deletes the folder AND all contents

DeleteFolder removes the folder, its files, and every subfolder in one shot — no manual emptying, no recursion to write. That power is also the danger: like Kill, it bypasses the Recycle Bin and there is no undo. It accepts wildcards in the last path element, and a second argument True forces deletion of read-only files. Treat fso.DeleteFolder as live ammunition — guard it exactly as hard as you would guard Kill, and never point it at a user-supplied path without checking.

The other error: RmDir on a missing folder

The second run-time error is the missing-folder case, and it does not match Kill's number:

RmDir "C:\Temp\Gone"    ' error 76 — "Path not found" if the folder does not exist
  • error 76 — the folder is not there. Guard with If Dir(path, vbDirectory) <> "" Then before removing, so a folder that is already gone is treated as success, not a crash.
  • error 75 — the folder is not empty (or is in use / read-only). Empty it first, or use fso.DeleteFolder.

Note the difference from Kill, which raises error 53 on a missing file; RmDir on a missing folder raises error 76. Same idea, different number, because one works on files and the other on folders — the existence check uses the vbDirectory flag for folders.

The safe delete pattern for an unattended macro

Putting the guards together, the folder delete that will not stall a scheduled run:

Sub SafeRemoveFolder(ByVal folder As String)
    If Dir(folder, vbDirectory) = "" Then Exit Sub    ' already gone — nothing to do
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFolder folder                            ' folder + all contents, in one call
End Sub

The Dir(folder, vbDirectory) guard absorbs the common "it isn't there" case so error 76 never fires, and fso.DeleteFolder handles a populated folder without hand-written recursion. If you specifically want the safe behaviour — refuse to delete anything non-empty — keep RmDir and let error 75 be your seatbelt. Choose the tool by how much you want to protect the contents.

The honest verdict: RmDir for empty, DeleteFolder for the tree

RmDir is the right tool for removing a folder you know is empty; anything else is a decision about how much safety you want. Four rules:

  • Empty folderRmDir path — one statement, and it refuses (error 75) if you were wrong about it being empty.
  • Folder of filesKill the files first, then RmDir the empty shell.
  • Folder tree, contents disposablefso.DeleteFolder — one call, but treat it like Kill: no Recycle Bin, no undo.
  • Guard existenceIf Dir(path, vbDirectory) <> "" Then so a missing folder is a no-op, not error 76.

The moment you catch yourself reaching for On Error Resume Next to make RmDir "just work," stop — that error is telling you the folder is not empty, and the fix is to decide, deliberately, whether its contents should survive.

How ExcelMaster helps

Deleting a folder safely means knowing that RmDir removes only an empty folder (error 75 otherwise), that it deletes folders but never files, that a missing folder is error 76 not 53, and that fso.DeleteFolder is the one call that wipes a whole tree — with no undo.

ExcelMaster writes the delete that matches your intent. Describe the job — "remove the temp folder after each run," or "clear last month's export tree" — and it produces the right code: RmDir with an existence guard for an empty folder, a Kill-then-RmDir pass when it holds files, or a guarded fso.DeleteFolder when the whole tree should go. You describe what should be left behind; it picks the tool that leaves it.

Frequently asked questions

How do I delete a folder in VBA?

Use the RmDir statement: RmDir "C:\Temp\Old". It removes the folder, but only if the folder is empty — a folder that still contains files or subfolders raises error 75. Guard it with an existence check first — If Dir("C:\Temp\Old", vbDirectory) <> "" Then RmDir "C:\Temp\Old" — so a folder that is already gone does not raise error 76.

Why does VBA RmDir give error 75?

Because the folder is not empty. RmDir deletes only an empty folder; if it still contains any file or subfolder, it stops with error 75, "Path/File access error." This is a safety feature that prevents a single line from wiping a full folder. To remove a populated folder, delete its files with Kill first, then RmDir the empty folder — or use FileSystemObject.DeleteFolder, which removes the folder and all of its contents in one call.

How do I delete a folder that is not empty in VBA?

RmDir cannot — it refuses a non-empty folder. Either empty it first (Kill "C:\Temp\Old\*.*" to delete the files, then RmDir "C:\Temp\Old", handling any subfolders recursively), or use the FileSystemObject: fso.DeleteFolder "C:\Temp\Old" deletes the folder and everything inside it in a single call. DeleteFolder bypasses the Recycle Bin and cannot be undone, so guard it as carefully as you would Kill.

What is the difference between RmDir and Kill in VBA?

They are opposites. Kill deletes files — any file, immediately, with no Recycle Bin — and raises error 53 on a missing file. RmDir deletes folders, but only empty ones, raising error 75 on a non-empty folder and error 76 on a missing one. Kill never deletes a folder and RmDir never deletes a file, so removing a folder full of files needs both: Kill the contents, then RmDir the shell.

Does RmDir move a folder to the Recycle Bin?

No. RmDir deletes the folder entry directly — there is no Recycle Bin and no undo, exactly like Kill for files. The same is true of FileSystemObject.DeleteFolder. Because none of these can be undone, confirm the folder is disposable before deleting, and guard the path so an unattended macro does not remove the wrong directory.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-08-23.

Related guides: VBA MkDir · VBA CurDir · VBA Delete File · VBA Check If File Exists · VBA FileSystemObject