Learn how to empty the recycle bin with c# .NET


Whatever the reason it is, this code snippet for clean the recycle bin is really useful and easy to implement, at least for learning purposes (or in case that you are working in the design of a maintenance application). Follow step by step and you'll be ready to clean the recycle bin with a simple click in a couple of minutes.

Cleaning the recycle bin

As first, include a reference to InteropServices with a use statement in the top of your class to use later theĀ DllImport method in our class.

using System.Runtime.InteropServices;
using System.Windows.Forms; // As we are using windows forms, we use this namespace to create a dialog to confirm our action

We are going to create a enum variable, this variable will contain the codes that our clear function will expect as parameters.

enum RecycleFlags : uint { 
   SHRB_NOCONFIRMATION = 0x00000001, // Don't ask confirmation
   SHRB_NOPROGRESSUI = 0x00000002, // Don't show any windows dialog
   SHRB_NOSOUND = 0x00000004 // Don't make sound, ninja mode enabled :v
}

Then we are going to create the function that will clear the recycle bin, we need to import the Shell32.dll , therefore we included before the System.Runtime.InteropServices namespace in order to use SHEmptyRecycleBin function from the .dll, now you just need to declare it.

[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

Now that the SHEmptyRecycleBin method is declared, we can use it to finally clean the recycle bin. We'll add a button that will trigger our function and we'll use the following code to create a confirmation dialog and finally clean the Recycle Bin.

// On click event of the button
private void button1_Click(object sender, EventArgs e)
{
    DialogResult result;
    result = MessageBox.Show("Are you sure you want to delete all the items in recycle bin", "Clear recycle bin", MessageBoxButtons.YesNo);

    // If accepted, continue with the cleaning
    if (result == DialogResult.Yes)
    {
        try
        {
            // Execute the method with the required parameters
            uint IsSuccess = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHRB_NOCONFIRMATION);
            MessageBox.Show("The recycle bin has been succesfully recycled !", "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {   
            // Handle exceptions
            MessageBox.Show("The recycle bin couldn't be recycled" + ex.Message, "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }
}

Recycle Bin

Complete class

This is how your class should look like, note that is an empty winforms project that assumes you have a single button with the button1 identifier and when it's clicked, it cleans the recycle bin:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace EmptyRecycleBin
{
    public partial class Form1 : Form
    {
        enum RecycleFlags : int
        {
            SHRB_NOCONFIRMATION = 0x00000001, // Don't ask for confirmation
            SHRB_NOPROGRESSUI = 0x00000001, // Don't show progress
            SHRB_NOSOUND = 0x00000004 // Don't make sound when the action is executed
        }

        [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
        static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure you want to delete all the items in recycle bin", "Clear recycle bin", MessageBoxButtons.YesNo);

            // If accepted, continue with the cleaning
            if (result == DialogResult.Yes)
            {
                try
                {
                    // Execute the method with the required parameters
                    uint IsSuccess = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHRB_NOCONFIRMATION);
                    MessageBox.Show("The recycle bin has been succesfully recycled !", "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    // Handle exceptions
                    MessageBox.Show("The recycle bin couldn't be recycled" + ex.Message, "Clear recycle bin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }
    }
}

Have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors