Operations 6 min read

Four Python Techniques to Safely Delete Folders on Windows

This article walks through four practical Python approaches—using the OS module, pathlib, shutil, and send2trash—to permanently or safely remove directories on Windows, explaining each method's behavior, required settings, and code examples for reliable folder deletion.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Four Python Techniques to Safely Delete Folders on Windows

Multiple Methods to Delete a Folder (S Series)

The S series ("Small") records handy, sometimes quirky operations; this entry focuses on deleting a directory that contains data files on a Windows system.

Platform

Windows 10

Python 3.8

pywin32 227 (or pypiwin32)

send2trash 1.5.0

Purpose

Remove a folder that holds data files.

Deletion Methods

All methods are performed on Windows.

Method 1 : Right‑click the folder and choose Delete, or select the folder and press the Delete key. This moves the folder to the Recycle Bin; you can restore it by clicking Restore.

Method 2 : Use Python's os module.

import os

delete_dir = r'测试文件夹'
for r, d, f in os.walk(delete_dir, topdown=False):
    for files in f:
        os.remove(os.path.join(r, files))  # delete files
    os.removedirs(r)  # delete empty folder

By setting topdown=False , os.walk traverses from the deepest level outward, allowing non‑empty folders to be emptied before removal.

Method 3 : Use the pathlib module.

from pathlib import Path

delete_dir = Path(r'测试文件夹')
# delete all files
[i.unlink() for i in delete_dir.rglob('*') if i.is_file()]
# delete all empty sub‑folders
[i.rmdir() for i in delete_dir.rglob('*') if i.is_dir()]
# delete the target folder itself
delete_dir.rmdir()

This separates file and folder deletion, performing two recursive passes before removing the top‑level directory.

Method 4 : Use shutil.rmtree.

from shutil import rmtree

delete_dir = r'测试文件夹'
rmtree(delete_dir)

rmtree recursively deletes all files and sub‑folders. It can be combined with safety checks similar to Method 2.

Safe Delete to Recycle Bin : Install send2trash (or use pywin32 shell functions) to move items to the Recycle Bin instead of permanent removal.

import send2trash

delete_dir = r'测试文件夹'
send2trash.send2trash(delete_dir)

Install the required packages with:

pip install send2trash
pip install pywin32   # or pip install pypiwin32

Alternatively, use win32com.shell to invoke the shell's recycle‑bin API.

from win32com.shell import shell, shellcon

def recyclebin_empty(confirm=True, show_progress=True, sound=True):
    flags = 0
    if not confirm:
        flags |= shellcon.SHERB_NOCONFIRMATION
    if not show_progress:
        flags |= shellcon.SHERB_NOPROGRESSUI
    if not sound:
        flags |= shellcon.SHERB_NOSOUND
    shell.SHEmptyRecycleBin(None, None, flags)

recyclebin_empty(False, False, False)

Summary

The article demonstrates several Python‑based techniques for deleting folders, ranging from manual Recycle Bin deletion to fully automated recursive removal with shutil.rmtree. While shutil.rmtree sometimes feels slower, tests showed that both shutil.rmtree and pathlib can achieve fast deletions depending on the environment.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

shutilOS modulepathlibsend2trash
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.