Content · Programming · Python

Python shutil.rmtree() Function: Usage and Examples

Delete a directory tree recursively with shutil.rmtree() only after validation. shutil.rmtree(path) removes a folder and everything underneath it; deletion is destructive and not sent to the recycle bin.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This Python tutorial explains shutil.rmtree() Function: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.

What This Feature Does


Delete a directory tree recursively with shutil.rmtree() only after validation.

shutil.rmtree(path) removes a folder and everything underneath it; deletion is destructive and not sent to the recycle bin. Use temporary test directories, strongly restrict the target, and never accept an unvalidated user path.

When to Use It


This technique belongs to file copying, temporary files and matching. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.

Step-by-Step Python Example


Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.

from pathlib import Path
from tempfile import TemporaryDirectory
import shutil

with TemporaryDirectory() as temporary_folder:
    root_folder = Path(temporary_folder)
    disposable_folder = root_folder / "generated-test-data"
    disposable_folder.mkdir()
    (disposable_folder / "placeholder.txt").touch()
    if disposable_folder.parent == root_folder and disposable_folder.name == "generated-test-data":
        shutil.rmtree(disposable_folder)
    print(disposable_folder.exists())

Understanding the Code


  • First, the imports make pathlib, tempfile, shutil available. They provide the filesystem, data or standard-library functions used later.
  • Find the call shutil.rmtree(disposable_folder). Its parameters determine the specific operation demonstrated in the title.
  • Its purpose is to delete a directory tree recursively with shutil.rmtree() only after validation. Compare this description against the result printed by the program.
  • The sample uses TemporaryDirectory() so any practice files are removed after the context ends; it does not work on your real documents.
  • The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.

Expected Output


False

The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.

Important Details and Edge Cases


On some platforms rmtree() protects against symlink attacks; verify runtime support if hostile directories are involved.

shutil.rmtree() permanently removes an entire directory tree. Do not test it on valuable data.

Practice: Try It Yourself


Create and remove an isolated folder under TemporaryDirectory.

Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.

Official Python Documentation


For the complete reference, see the Python standard library documentation.

Common Beginner Mistakes


  • Passing a path derived from unchecked input into an irreversible recursive deletion.
  • Assuming rmtree() is equivalent to deleting only an empty folder with rmdir().
  • shutil.rmtree() permanently removes an entire directory tree. Do not test it on valuable data.

Key Takeaways


  • Delete a directory tree recursively with shutil.rmtree() only after validation.
  • shutil.rmtree(path) removes a folder and everything underneath it; deletion is destructive and not sent to the recycle bin. Use temporary test directories, strongly restrict the target, and never accept an unvalidated user path.
  • Avoid this common pitfall: Passing a path derived from unchecked input into an irreversible recursive deletion.
Share this resourceSend this page to someone who may find it useful.