Skip to main content

How to Delete a Folder and Everything in It in the Terminal

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

rm on its own won't delete a folder. Add -r and it deletes the folder with every file and folder inside it, and add -f as well and it does that without a single question or message. In PowerShell, Remove-Item -Recurse -Force does the same.

Neither command asks you to confirm, so this guide practises on a folder it creates for the purpose, with real output from zsh 5.9, bash 3.2 and PowerShell 7.6 on macOS.

Syntax​

In bash and zsh:

Syntax
rm -rf <folder>

In PowerShell:

Syntax
Remove-Item -Recurse -Force <folder>
PartWhat it means
rmShort for remove. Deletes files
-rDeletes folders too, with everything inside them, every level down
-fNever asks, and says nothing when the name doesn't exist
Remove-ItemPowerShell's command for deleting files and folders
-RecurseDeletes everything inside the folder too
-ForceAlso deletes hidden and read-only files
<folder>The folder to delete

The macOS manual page, man rm, describes -R as Attempt to remove the file hierarchy rooted in each file argument., and -r as Equivalent to -R. For -f: Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error.

Microsoft's documentation for Remove-Item describes -Recurse: Indicates that this cmdlet deletes the items in the specified locations and in all child items of the locations. And -Force: Forces the cmdlet to remove items that can't otherwise be changed, such as hidden or read-only files or read-only aliases or variables.

The Example​

You create a practice folder, old-project, with a file in it and a drafts folder holding another file, and then delete all of it.

Step 1: Create a Folder to Practise On​

Terminal
mkdir -p old-project/drafts
echo 'Read me first' > old-project/readme.txt
echo 'An idea' > old-project/drafts/idea.txt
ls -R old-project
Output
drafts readme.txt

old-project/drafts:
idea.txt

mkdir -p creates old-project and the drafts folder inside it. Each echo line writes a line of text into a new file, and ls -R lists the folder and every folder inside it.

Step 2: Try It Without -r​

Terminal
rm old-project
Output
rm: old-project: is a directory

rm refused, and the folder is still there. Without -r, it deletes only files. Its manual: The rm utility attempts to remove the non-directory type files specified on the command line.

Step 3: Delete It with -r and -f​

Terminal
rm -rf old-project
ls old-project

macOS-style terminal window titled Terminal with rm -rf old-project, which deletes the folder and everything in it, followed by ls old-project to check that it's gone.

Output
ls: old-project: No such file or directory

rm -rf printed nothing. ls lists what's in a folder, and here it says the folder doesn't exist: both files and the drafts folder went with it. The manual's own example is the same command: Recursively remove all files contained within the foobar directory hierarchy.

macOS-style terminal window titled Terminal showing a real zsh session: the practice folder is created and listed with ls -R, rm old-project fails with is a directory, and rm -rf old-project deletes it.

bash printed the same as zsh in every step.

Step 4: See What -f Hides​

Terminal
rm -r missing-folder
echo $?
rm -rf missing-folder
echo $?
Output
rm: missing-folder: No such file or directory
1
0

echo $? prints the exit status of the command before it. Without -f, rm named the folder it couldn't find and ended with 1, an error. With -f, it said nothing and ended with 0. The manual: The rm utility exits 0 if all of the named files or file hierarchies were removed, or if the -f option was specified and all of the existing files or file hierarchies were removed. So with -f, a mistyped name looks exactly like a folder that was deleted.

Common Mistakes​

MistakeWhat happensDo this instead
A name with a space, not quoted: rm -rf old projectNothing printed, and nothing was deleted: ls still showed old projectQuote the name: rm -rf 'old project' deleted it
rm old-project without -rrm: old-project: is a directory, and the folder staysAdd -r
Remove-Item old-project without -RecursePowerShell asks Are you sure you want to continue?Add -Recurse

In the first row, rm looked for two items, old and project. Neither exists, and -f kept quiet about both.

FAQ​

How do I see what rm deletes?​

Add -v. rm -rv old-project printed each item as it went:

Output
old-project/drafts/idea.txt
old-project/drafts
old-project/readme.txt
old-project

The manual: Be verbose when deleting files, showing them as they are removed.

Can it ask me before it deletes?​

Yes. In bash and zsh, use -I in place of -f: rm -rI old-project asked recursively remove old-project?, and answering n kept the folder. The manual: Request confirmation once if more than three files are being removed or if a directory is being recursively removed.

In PowerShell, -WhatIf shows what would happen without doing it. Remove-Item -Recurse -Force old-project -WhatIf printed What if: Performing the operation "Remove Directory" on target "/Users/you/delete-demo/old-project"., and the folder stayed. The documentation: Shows what would happen if the cmdlet runs. The cmdlet isn't run.

Conclusion​

rm -rf old-project deleted the folder together with both files and the drafts folder inside it, and Remove-Item -Recurse -Force old-project did the same in PowerShell. Check the name before you press Enter: with -f, a wrong name deletes nothing and says nothing.

Additional Resources​