Delete Files and Folders

Maintained on
Table of Contents

When cleaning up in a batch file, the command you use depends on whether the target is a file or a folder.

In Command Prompt, use them as follows.

What to deleteCommand
A filedel
An empty folderrmdir
A folder together with its contentsrmdir /s
Deletion in Command Prompt is basically irreversible

Items deleted with del or rmdir normally do not go to the Recycle Bin. Always double-check the target path before running them.

Delete files

Use the del command to delete files.

Delete a single file

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del "%USERPROFILE%\Documents\disposable.txt"

Delete multiple files

Separate multiple files with spaces.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del "%USERPROFILE%\Documents\output.txt" "%USERPROFILE%\Documents\input.txt"

Delete files in bulk with wildcards

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del "%USERPROFILE%\Documents\sample*.txt"

Confirm before deleting

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del /p "%USERPROFILE%\Documents\output.txt"

Delete a read-only file

A file with the read-only attribute cannot be deleted as is. Add /f to force deletion.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del /f "%USERPROFILE%\Documents\readonly.txt"

Delete files in subfolders too

With /s, matching files in subfolders are deleted as well. The folders themselves remain.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>del /s "%USERPROFILE%\Documents\disposable\*.tmp"
del does not delete the folder itself

del deletes files, not folders. Note that if you specify a folder, all files directly inside it are deleted. To delete a folder itself, use rmdir.

Delete folders

Use the rmdir command to delete folders. Its short form rd works the same way.

Delete an empty folder

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>rmdir "%USERPROFILE%\Documents\disposable"

Delete a folder with its contents

If the folder contains files or subfolders, add /s.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>rmdir /s "%USERPROFILE%\Documents\disposable"

To suppress the confirmation message as well, add /q.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>rmdir /s /q "%USERPROFILE%\Documents\disposable"
Do not use rmdir to delete a single file

rmdir is a command for directories. Use del to delete a single file.

Example: batch deletion in a batch file

@echo off
setlocal

set "WORKDIR=%USERPROFILE%\Documents\disposable"

del /q "%WORKDIR%\*.tmp" 2>nul
if exist "%WORKDIR%" rmdir /s /q "%WORKDIR%"

endlocal
exit /b

This example first deletes the temporary files, then deletes the working folder together with its contents.

When deletion fails, and how to avoid mistakes

  • Hidden files are not deleted: by default del skips files with the hidden attribute. Add /a, as in del /f /a "path\*".
  • Never run rmdir /s /q with an empty variable: when you build the path from a variable such as rmdir /s /q "%WORKDIR%", if you append a path such as "%WORKDIR%\sub", an unset variable turns it into \sub at the drive root. Check first with if not defined WORKDIR exit /b 1.
  • “In use” errors: deletion fails while another program has the file or folder open. Close that program and run the command again.

Exercises

Practice Problem

Which command deletes a folder together with the files inside it?

Choose one answer.

  1. del "%USERPROFILE%\Documents\disposable"
  2. rmdir "%USERPROFILE%\Documents\disposable"
  3. rmdir /s "%USERPROFILE%\Documents\disposable"

Practice Problem

Which command deletes a single file?

Choose one answer.

  1. del file.txt
  2. rmdir file.txt
  3. mkdir file.txt
Try more practice questions 171 questions by topic. Answer in your browser and check instantly. Start practicing →
#Command Prompt #Batch Files #File Operations