How to Delete a Directory via Command Line in Ubuntu

The Recursive Deletion Vector

On an Ubuntu Linux server, managing storage requires precision. Deleting a single file via the terminal is straightforward, but deleting an entire directory (folder) that is full of subdirectories and files is inherently more dangerous. The operating system actively protects directories to prevent catastrophic accidental data loss. If you attempt to use the standard file removal command (rm) on a folder, Linux will simply return an error stating “Is a directory,” refusing to execute the action.

How to Delete a Directory

You must instruct the removal command to behave recursively, forcing it to burrow into the directory and systematically destroy every file it contains before finally deleting the folder itself.

1. Open your terminal application or connect to your server via SSH.
2. To cleanly delete an empty directory, use the rmdir (remove directory) command:

rmdir /path/to/empty_folder

3. However, if the folder contains any data whatsoever, rmdir will fail. You must use the rm command combined with the -r (recursive) flag. To delete a folder named old_backups, execute:

rm -r old_backups/

4. If the directory contains protected system files, or if you simply do not want the terminal to pause and ask you to confirm every single deletion, you must add the -f (force) flag. WARNING: This command is absolute and irreversible.

rm -rf /path/to/folder/

The terminal will instantly obliterate the directory and all of its contents, returning to a blank prompt upon successful execution.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.