(Print this page)

Emptying the Recycling bin from a .Net application
Published date: Saturday, December 21, 2019
On: Moer and Éric Moreau's web site

I have lazy users! Some of them, not all. They are not good at cleaning what they are deleting (either files or Outlook items). Or maybe I just pamper them too much.

I had a use who had issues lately because the C: drive of his small SSD was almost full and the user was very good at ignoring the warnings sent by Windows.

I found a way of cleaning the Recycle from a .Net application and it is surprisingly it is very simple.

The downloadable code

This month’s solution contains both VB and C# projects. The solution was created using Visual Studio 2019, but the code should work as well in older versions of the .Net Framework because there isn’t really nothing very fancy.

Figure 1: The demo application in action

A reference is needed – maybe!

Per say, you don’t need to add the following reference if you only want to empty the recycling bin.

But if you want to list the content of the recycle bin, you need to add a reference to the Shell32 component. To add this reference, click on the Browse button and navigate to your C:\Windows\System32 to find the Shell32.dll file.

Figure 2: Adding the missing reference to list the content of the Recycling bin

Listing the content of the Recycling bin

Before deleting the content of the recycling bin, let’s start by showing how to list its content.

It is quite easy when using the Shell component. We must create a new instance of that object and call the NameSpace method specifying that we want to work with the BitBucket folder. This will return the collection of items currently deleted.

In my demo, I just loop through that collection and display the items’ name, size and date in the listbox.

Private Sub btnListContent_Click(sender As Object, e As EventArgs) Handles btnListContent.Click

    Dim objShell As New Shell()
    Dim recycleBin As Folder = objShell.NameSpace(ShellSpecialFolderConstants.ssfBITBUCKET)
    For Each item As FolderItem2 In recycleBin.Items()
        listBox1.Items.Add($"{item.Name} - {item.Size} bytes - modified {item.ModifyDate:yyyy/MM/dd}")
End Sub
private void btnListContent_Click(object sender, EventArgs e)

    Shell shell = new Shell();
    Folder recycleBin = shell.NameSpace(ShellSpecialFolderConstants.ssfBITBUCKET);
    foreach (FolderItem2 item in recycleBin.Items())
        listBox1.Items.Add($"{item.Name} - {item.Size} bytes - modified {item.ModifyDate:yyyy/MM/dd}");

Emptying the Recycling bin

This time, we will need to call another method from the Shell32.dll but using a different way, the reference is not needed. We need to declare a proxy to a method and some constants contained in that DLL.

 Enum RecycleFlag
     SHERB_NOCONFIRMATION = &H1 'No confirmation, when emptying
     SHERB_NOPROGRESSUI = &H2 'No progress tracking window during the emptying of the recycle bin
     SHERB_NOSOUND = &H4 'No sound when the emptying of the recycle bin is complete
 End Enum

 Private Shared Function SHEmptyRecycleBin(ByVal hwnd As IntPtr, ByVal pszRootPath As String, ByVal dwFlags As RecycleFlag) As Integer
 End Function
enum RecycleFlag : int
    SHERB_NOCONFIRMATION = 0x00000001, // No confirmation, when emptying
    SHERB_NOPROGRESSUI = 0x00000002,   // No progress tracking window during the emptying of the recycle bin
    SHERB_NOSOUND = 0x00000004         // No sound when the emptying of the recycle bin is complete

static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);

After you have these declarations at the top of your file, you are ready to use them.

Private Sub btnEmptyRecycleBin_Click(sender As Object, e As EventArgs) Handles btnEmptyRecycleBin.Click
    Dim hresult As Integer = SHEmptyRecycleBin(IntPtr.Zero, Nothing,
                                               RecycleFlag.SHERB_NOSOUND Or RecycleFlag.SHERB_NOCONFIRMATION Or RecycleFlag.SHERB_NOPROGRESSUI)
End Sub
private void btnEmptyRecycleBin_Click(object sender, EventArgs e)
    int hresult = SHEmptyRecycleBin(IntPtr.Zero, null, 


So, I don’t know if I pamper my users too much or if I am just making my life easier by automating as much as I can but in the end, that users will never complain again for such an easy workaround!

Maybe you will decide to teach your users how to have good computer hygiene (I gave up with a few of mine) but this hint can definitely becomes handy on a server!

(Print this page)