How to Empty Recycle Bin using Python?
Last Updated :
28 Apr, 2025
In this article, we are going to learn how to empty recycle bin using Python.
We are going to use winshell module of Python to empty the recycle bin and before making recycle bin empty we are going to check how many elements are present in recycle bin and their names.
Winshell module
The Winshell module is a lightweight wrapper around the Windows Shell functionality. Microsoft categorizes some Windows user interface components as Shell functions. Some examples are the Desktop, shortcut icons, individual folders (like My Documents), and organized storage. Simple functions that provide reasonable defaults for the majority of the parameters are used to expose each of them.
Uses: The module provides the following functional areas:
- Specialized Files
- File shortcuts for operations
- Recyclable Storage Container
It is used to provide access to specific folders, uses the shell's file duplicate, rename, & delete functionality, and includes limited support for structured storage.
Run the following command in the Terminal to install the module:
pip install winshell
winshell.recycle_bin( ).empty( ) function
The winshell.recycle_bin( ).empty( ) will return a ShellRecycleBin object that represents the System Recycling Binand the object (which is returned from a call to recycle_bin( )) is iterable and it empties all system recycle bin, optionally prompting for confirmation, displaying progress, and playing a crunching sound through its parameters that take a boolean value.
Syntax:
empty(confirm=True, show_progress=True, sound=True)
Parameters Used:
- confirm: This takes the input of a boolean value either true or false and indicates the confirmation of the deletion process.
- show_progress: This indicates whether to display the progress of the deletion process at every instant and works based on the input of a boolean value.
- sound: This parameter enters a boolean value for its procedure and assists in producing a crunching sound to indicate the deletion process's completion.
View items in recycle bin using Python
To view the all items in recycle bin we are going to use the recycle_bin() function. This function returns an object and then we convert this object into a list and after that print all list items using for loop in Python.
Python3
# Importing required module
import winshell
# Convert the objects returned by recycle_bin()
# into list and stored in recycleBin_items
recycleBin_items = list(winshell.recycle_bin())
# Print no. of elements in recycle bin
print(len(recycleBin_items),
"items are present in recycle bin")
# Print name of all items with their
# recycled date
for item in recycleBin_items:
print(item)
Output:
Deleting all items from recycle bin using Python
To delete all items from recycle bin we are going to use recycle_bin().empty() function and we also used the try-except block to handle the error when the recycle bin is already empty.
Python3
# Importing the required modules
import winshell
# Using try and except block to handle exceptions
try:
# Calling the built-in function to delete
# the data inside Recycle-Bin
winshell.recycle_bin().empty(confirm=False,
show_progress=False, sound=True)
# Print that the deletion in successful
print("Recycle Bin is emptied now!")
except:
# Printing that the Recyclce-Bin is already Empty!
print("Recycle Bin is already empty!")
# This code is contributed by PL VISHNUPPRIYAN
Before running the code:
After running the code:
Terminal Output:
Terminal Output:
Similar Reads
Releasing Memory in Python Python's memory management is primarily handled by its built-in garbage collector (GC), which automatically deallocates memory that is no longer in use. However, to optimize memory usage, developers can employ explicit techniques to manage memory more effectively, especially in long-running or memor
4 min read
Python | RecycleView in Kivy Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. RecycleView: Recycleview helps to deal with a large number of da
2 min read
Create an empty file using Python File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with
3 min read
How to Uninstall Software using Python? In this article, we are going to learn how to uninstall software using Python. Performing operations on the operating system are one of the most common use cases of Python language. The inclusions of the os library offer application processing capabilities to the Python programming language. Uninsta
3 min read
Python | shutil.disk_usage() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories. shutil.disk_usage() method in Python is to get disk u
2 min read
Python | shutil.disk_usage() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories. shutil.disk_usage() method tells the disk usage stati
2 min read