TExcelTAD
Home › Terminal & Command Line › Deleting Safely

Deleting Safely

Deleting from the command line is fast and permanent: del, Remove-Item and rm don't use the Recycle Bin or the Trash. There is no undo. This lesson shows how to delete files and folders in each shell, and, just as important, the habits that keep you from deleting the wrong thing.

Task CMD PowerShell bash / zsh
Delete a file del a.txt (erase) Remove-Item a.txt (del, rm, ri) rm a.txt
Delete several files del *.tmp Remove-Item *.tmp rm *.tmp
Delete an empty folder rd old (rmdir) Remove-Item old rmdir old
Delete a folder and everything in it rd /s /q old Remove-Item old -Recurse -Force rm -r old (rm -rf old)
Ask before each file del /p *.tmp Remove-Item *.tmp -Confirm rm -i *.tmp
Preview only dir /b *.tmp first -WhatIf ls *.tmp / echo rm *.tmp first
Delete in all sub-folders del /s /q *.tmp Get-ChildItem -Recurse *.tmp | Remove-Item find . -name '*.tmp' -delete

Windows (CMD)

del removes files; rd (or rmdir) removes folders. rd only removes empty folders unless you add /s (sub-folders and files), and /q (quiet) skips the Are you sure (Y/N)? question:

cmd · try it yourself
@echo off
mkdir cleanup\cache\old
cd cleanup
echo a> notes.txt
echo b> temp1.tmp
echo c> temp2.tmp
echo d> cache\data.bin
del temp1.tmp
del /q *.tmp
dir /b /o:n
echo ---
rd /s /q cache
dir /b /o:n
cache
notes.txt
---
notes.txt

Things to watch out for in CMD:

  • del on a folder name deletes the files inside it (after asking Are you sure?) but keeps the folder. Use rd /s to remove the folder itself.
  • del skips read-only files unless you add /f, and it doesn't touch hidden files unless you use /a.
  • rd /s /q asks nothing at all. Double-check the path, especially when it comes from a variable: if %target% is empty, rd /s /q "%target%\" would refer to the root of the current drive.
cmd
C:\Users\ada\cleanup>del cache
C:\Users\ada\cleanup\cache\*, Are you sure (Y/N)? n

Windows (PowerShell)

Remove-Item deletes files and folders. A folder with content needs -Recurse; read-only and hidden items need -Force. Its best feature is -WhatIf: run the command with it first, read the list, then run it again without:

powershell · try it yourself
New-Item -ItemType Directory cleanup/cache -Force | Out-Null
Set-Location cleanup
Set-Content notes.txt 'a'
Set-Content temp1.tmp 'b'
Set-Content temp2.tmp 'c'
Set-Content cache/data.bin 'd'

Remove-Item *.tmp -WhatIf
Remove-Item *.tmp
Remove-Item cache -Recurse
Get-ChildItem -Name
What if: Performing the operation "Remove File" on target "/tmp/cleanup/temp1.tmp".
What if: Performing the operation "Remove File" on target "/tmp/cleanup/temp2.tmp".
notes.txt

Without -Recurse, removing a non-empty folder stops and asks The item at … has children … Are you sure you want to continue?, which fails in a non-interactive script. -Confirm asks before every single item.

Because PowerShell works with objects, you can filter precisely before deleting, for example "only .log files bigger than 1 MB" or "older than 30 days":

powershell
PS C:\Users\ada> Get-ChildItem C:\Temp -Recurse -File |
>>   Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) |
>>   Remove-Item -WhatIf

Linux & macOS (bash/zsh)

rm removes files, rm -r removes folders with their content, and rmdir removes only empty folders (a nice safety net):

bash · try it yourself
mkdir -p cleanup/cache/old && cd cleanup
echo a > notes.txt
echo b > temp1.tmp
echo c > temp2.tmp
echo d > cache/data.bin
rm temp1.tmp
rm -v *.tmp
rmdir cache
rm -r cache
ls
removed 'temp2.tmp'
rmdir: failed to remove 'cache': Directory not empty
notes.txt
  • -v reports every file removed; -i asks before each one; -I asks once before deleting more than three files.
  • -f (force) never asks and ignores missing files. rm -rf is therefore the most dangerous command on a Unix system: it deletes a whole tree without any question.
  • A file name starting with - looks like an option. Use -- (end of options) or ./: rm -- -draft.txt or rm ./-draft.txt.
bash · try it yourself
touch -- -draft.txt keep.txt
rm -- -draft.txt
ls
keep.txt

zsh adds its own guard: before running rm * or rm path/* interactively, it asks sure you want to delete all … files?. Both shells can also preview a wildcard by putting echo in front of the command:

zsh · try it yourself
touch report.txt draft1.tmp draft2.tmp
echo rm *.tmp
rm *.tmp
print -l *
rm draft1.tmp draft2.tmp
report.txt

Because the shell expands *.tmp before the command runs, echo shows exactly what rm will receive.

Safety habits for every shell

  1. Look first. Run the listing with the same pattern (dir /b *.tmp, Get-ChildItem *.tmp, ls *.tmp) before deleting.
  2. Preview. Use -WhatIf (PowerShell), echo rm … (bash/zsh) or zmv -n.
  3. Avoid empty variables in paths. In bash, rm -rf "$dir/"* with an empty $dir targets /. Use "${dir:?}", which stops the script if the variable is empty (example below).
bash · try it yourself
dir=""
rm -rf "${dir:?is empty, refusing to delete}/"*
echo "not reached"
/w/main.sh: line 2: dir: is empty, refusing to delete
  1. Prefer the Trash for manual cleanups. Graphical file managers move files to the Recycle Bin/Trash. From a Linux terminal, gio trash file does the same; on macOS and Windows install a helper (brew install trash, the Recycle module) if you want that safety from the command line.
  2. Never run a delete command you copied from the internet without understanding every part of it, especially with sudo or in an administrator window.

Learn more: rm: Remove Files · del: Delete Files · mkdir & rmdir · PowerShell Files & Folders

Test yourself

Where do files deleted with rm, del or Remove-Item go?

  1. The Recycle Bin / Trash
  2. A hidden backup folder
  3. Nowhere: they're deleted permanently
  4. The parent folder

Which CMD command deletes the folder old with all its sub-folders and files without asking?

  1. del old
  2. rd old
  3. rd /s /q old
  4. del /s old

How can you see which files rm *.log would delete, without deleting them?

  1. rm -n *.log
  2. echo rm *.log
  3. rm --preview *.log
  4. rm *.log -WhatIf