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:
@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:
C:\Users\ada\demo>del draft.docx
Could Not Find C:\Users\ada\demo\draft.docxWildcards 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:
C:\Users\ada\demo>del *.*
C:\Users\ada\demo\*.*, Are you sure (Y/N)? nScripts 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:
*.htmalso matchespage.html, and*.docmatchesfile.docx, because Windows also compares the old 8.3 short names. Check withdir /b patternbefore 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:
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)? nDeleting 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:
@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 /sC:\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:
@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 cacheC:\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:
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
- Preview first. Run
dir /b(ordir /b /s) with the exact same pattern and read the list. - Quote paths with spaces:
del "C:\Temp\old report.txt". - Guard variables.
del /q "%folder%\*"with an emptyfolderbecomesdel /q "\*", which empties the root of the drive. Check withif not defined folder exit /b 1first. - Use full paths in scripts, or
cd /dto a known folder first, so a script started from the wrong place can't delete the wrong files.
@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?
- To the Recycle Bin
- Nowhere: it is deleted permanently
- To C:\Windows\Temp
- To a hidden .trash folder
What does del /q reports do when reports is a folder?
- Deletes the folder and everything in it
- Deletes the files directly inside reports, keeping the folder and subfolders
- Prints an error
- Deletes only empty subfolders
Which switch makes del also delete matching files in subfolders?
- /r
- /s
- /a
- /all