Applies to: rmdir and rm on Ubuntu, Debian, AlmaLinux, Rocky Linux and other Linux systems
Linux has two commands for deleting folders: rmdir for empty folders and rm -r for folders with files in them. There is no recycle bin on the command line, so a deleted folder is gone. We tested the commands below on Ubuntu 24.04, Debian 13 and AlmaLinux 9.
Remove an empty folder
rmdir old-folder
If the folder isn’t empty you get rmdir: failed to remove 'old-folder': Directory not empty, which makes rmdir a safe choice.
Remove a folder and everything in it
rm -r old-folder # remove the folder, its files and subfolders rm -ri old-folder # ask before removing each item rm -rf old-folder # no questions, also removes write-protected files
Use -f only when you are sure of the path. Before running it, check what you are about to delete:
ls -la old-folder du -sh old-folder
Remove only the files inside a folder
rm old-folder/* # files only (not hidden ones or subfolders) find old-folder -mindepth 1 -delete # everything inside, keep the folder
Safety tips
- Double-check paths that start with
/, and never runrm -rfwith a variable that could be empty: if$DIRis empty,rm -rf "$DIR"/*becomesrm -rf /*. In scripts, use"${DIR:?}", which stops with an error when the variable is empty. - Take a backup first when deleting website or database folders.
- “Permission denied” means you don’t own the files; use
sudoonly if you are sure they should go.
In cPanel
In File Manager, select the folder and click Delete. It goes to the Trash (which still uses disk space) unless you tick Skip the trash and permanently delete the files.
More: what the rm -rf command does and how to find and remove folders recursively.
Ucartz services for this topic
- KVM VPS hosting: NVMe SSD and full root access, with Free Basic Managed Support.
- Unmanaged dedicated servers: full root access and free IPMI KVM, with Free Basic Managed Support.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
