How to rm -rf in Python to Delete a Directory?

5/5 - (1 vote)

Problem Formulation: How to Remove a Directory in Python?

The rm command in Linux removes a specific directory. You can also add the options

  • -r remove the directory recursively
  • -f ignore nonexistent files and arguments and don’t prompt the user to ask for confirmation

So, if you run rm -rf my_directory, it’ll forcefully remove my_directory and all its child directories.

$ rm -rf my_directory

What is the best way to do the equivalent of rm -rf in Python?

Method 1: shutil.rmtree()

shutil.rmtree() to remove directories in Python

The most Pythonic way to rm -rf is to use the function shutil.rmtree() defined in the shutil package. It takes one argument, the folder to be removed, and removes the folder recursively.

import shutil

Note that rmtree isn’t semantically identical to rm -rf because it raises an error if you try to remove a single file.

If you generally want to suppress error messages, you can use the following command instead:

shutil.rmtree('my_directory', ignore_errors=True)

Method 2: os.unlink() and shutil.rmtree()

The following method resolves this problem of Python raising an error if you try to remove a single file.

import os, shutil

def remove(path):
    if os.path.exists(path):  
        if os.path.isfile(path) or os.path.islink(path):


As pointed out here, this method works on symbolic links to directories in the directory to be removed.

Method 3: os.walk()

You can also remove a directory with all its content by using the os.walk() method that goes over all files and folders in a given directory.

# CAUTION: top == '/' could delete all files on your disk!
import os
my_dir = '/my_directory'
for root, dirs, files in os.walk(my_dir, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

Method 4: Remove Read Only Files in Windows

You can also remove a directory in Windows if files are in a read only mode by using the following code from the Python docs:

import os, stat, shutil

def remove_readonly(func, path):
    os.chmod(path, stat.S_IWRITE)

directory = 'my_dir'
shutil.rmtree(directory, onerror=remove_readonly)

This example shows how to remove a directory tree named 'my_dir' on Windows where some of the files are read-only. The function is used as a callback to clear the readonly bit and reattempt the removal process. If there’s still an error, it’ll be propagated up to the main program.

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!