Open In App

How to Empty Recycle Bin using Python?

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads