TExcelTAD
Home › Windows CMD & Batch › del: Delete Files

del: Delete Files

del (or its synonym erase) deletes files. It is fast, it accepts wildcards, and it does not use the Recycle Bin: a deleted file is gone unless you have a backup. This lesson shows how to use it precisely and safely.

Deleting one or more files

Give del one or more file names or patterns:

cmd · try it yourself
@echo off
mkdir demo & cd demo
for %%n in (a.tmp b.tmp notes.txt old.log report.txt) do echo x> %%n
del old.log
del *.tmp
dir /b /o:n
echo ---
del notes.txt report.txt
set count=0
for %%f in (*) do set /a count+=1
echo files left: %count%
notes.txt
report.txt
---
files left: 0

del itself prints nothing when it succeeds. If nothing matches, Windows tells you, showing the full path it looked for:

cmd
C:\Users\ada\demo>del draft.docx
Could Not Find C:\Users\ada\demo\draft.docx

Wildcards and the "Are you sure?" question

Patterns such as *.tmp or report-??.csv delete every matching file without asking. Only the pattern that matches everything, * or *.*, triggers a safety question:

cmd
C:\Users\ada\demo>del *.*
C:\Users\ada\demo\*.*, Are you sure (Y/N)? n

Scripts usually add /q (quiet) to skip that question. Use it only when the path is certainly right.

Warning: Wildcards with three-letter extensions have a legacy quirk: *.htm also matches page.html, and *.doc matches file.docx, because Windows also compares the old 8.3 short names. Check with dir /b pattern before deleting.

Asking before each file: /p

/p (prompt) asks for each file, which is handy when a pattern matches more than you are sure about:

cmd
C:\Users\ada\demo>del /p *.log
C:\Users\ada\demo\app.log, Delete (Y/N)? y
C:\Users\ada\demo\keep.log, Delete (Y/N)? n

Deleting in subfolders: /s

/s deletes matching files in the folder and all of its subfolders. On Windows it lists every file it deletes (Deleted file - C:\…), so scripts send that list to nul unless they want a log:

cmd · try it yourself
@echo off
mkdir demo\src\lib & cd demo
echo code> src\main.c
echo obj> src\main.obj
echo code> src\lib\util.c
echo obj> src\lib\util.obj
del /s /q *.obj >nul
dir /b /o:n /s
C:\Users\learner\demo\src
C:\Users\learner\demo\src\lib
C:\Users\learner\demo\src\main.c
C:\Users\learner\demo\src\lib\util.c

The folders survive: del never removes directories. Use rd /s /q for that.

del on a folder name

Watch out for this one: if you give del the name of a folder, it deletes the files inside that folder (after asking, unless /q is given) and keeps the folder and its subfolders:

cmd · try it yourself
@echo off
mkdir demo\cache\thumbs & cd demo
echo one> cache\a.dat
echo two> cache\b.dat
echo three> cache\thumbs\c.png
del /q cache
dir /b /o:n /s cache
C:\Users\learner\demo\cache\thumbs
C:\Users\learner\demo\cache\thumbs\c.png

a.dat and b.dat are gone, while the subfolder thumbs and its file are untouched (that would need /s).

Read-only and hidden files: /f and /a

By default del refuses to delete read-only files and skips hidden and system files:

cmd
C:\Users\ada\demo>del setup.log
Access is denied.

C:\Users\ada\demo>del /f setup.log

C:\Users\ada\demo>
Switch Meaning
/p prompt before each file
/q quiet: no "Are you sure?" for *
/s also delete matching files in all subfolders
/f force deletion of read-only files
/a:h select files by attribute (h hidden, r read-only, s system, a archive; - means "not")

The attrib lesson explains file attributes.

Safe deleting habits

  1. Preview first. Run dir /b (or dir /b /s) with the exact same pattern and read the list.
  2. Quote paths with spaces: del "C:\Temp\old report.txt".
  3. Guard variables. del /q "%folder%\*" with an empty folder becomes del /q "\*", which empties the root of the drive. Check with if not defined folder exit /b 1 first.
  4. Use full paths in scripts, or cd /d to a known folder first, so a script started from the wrong place can't delete the wrong files.
cmd · try it yourself
@echo off
mkdir demo\tmp & cd demo
for %%n in (x1.tmp x2.tmp keep.txt) do echo data> tmp\%%n
set "folder=%cd%\tmp"
if not defined folder (echo folder is not set & exit /b 1)
echo These files will be deleted:
dir /b /o:n "%folder%\*.tmp"
del /q "%folder%\*.tmp"
echo Left over:
dir /b /o:n "%folder%"
These files will be deleted:
x1.tmp
x2.tmp
Left over:
keep.txt

Command summary

Task CMD PowerShell bash
delete a file del a.txt Remove-Item a.txt rm a.txt
delete by pattern del *.tmp Remove-Item *.tmp rm *.tmp
ask for each file del /p *.log Remove-Item *.log -Confirm rm -i *.log
recursive by pattern del /s /q *.obj Get-ChildItem -Recurse *.obj | Remove-Item find . -name '*.obj' -delete
read-only files del /f Remove-Item -Force rm -f

Learn more: Deleting Safely · Bash rm

Test yourself

Where does a file deleted with del go?

  1. To the Recycle Bin
  2. Nowhere: it is deleted permanently
  3. To C:\Windows\Temp
  4. To a hidden .trash folder

What does del /q reports do when reports is a folder?

  1. Deletes the folder and everything in it
  2. Deletes the files directly inside reports, keeping the folder and subfolders
  3. Prints an error
  4. Deletes only empty subfolders

Which switch makes del also delete matching files in subfolders?

  1. /r
  2. /s
  3. /a
  4. /all