Empty Recycle bin

tayspen 0 Tallied Votes 250 Views Share

Code to empty the windows recycle bin

enum RecycleFlags : uint
        {
            SHERB_NOCONFIRMATION = 0x00000001,
            SHERB_NOPROGRESSUI = 0x00000001,
            SHERB_NOSOUND = 0x00000004
        }

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


//The follwing code does the emying
 uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);


//Here is a full app, on load it empties the bin.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//Need for DLL import

using System.IO;

namespace RBinTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
            //empties on load
        }

        enum RecycleFlags : uint
        {
            SHERB_NOCONFIRMATION = 0x00000001,
            SHERB_NOPROGRESSUI = 0x00000001,
            SHERB_NOSOUND = 0x00000004
        }

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

Dani AI

Generated

's post shows the right API to call, but there are a few practical fixes and safety notes that make the snippet robust.

Fixes and a safe usage pattern (correct flag values, single definition, check result):

[Flags]
enum RecycleFlags : uint
{
    SHERB_NOCONFIRMATION = 0x00000001,
    SHERB_NOPROGRESSUI   = 0x00000002,
    SHERB_NOSOUND        = 0x00000004
}

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

// example call (no confirmation, no sound)
int hr = SHEmptyRecycleBin(IntPtr.Zero, null, (uint)(RecycleFlags.SHERB_NOCONFIRMATION | RecycleFlags.SHERB_NOSOUND));
if (hr != 0) Marshal.ThrowExceptionForHR(hr);

Notes and troubleshooting:

  • The posted enum accidentally assigns SHERB_NOPROGRESSUI the same value as SHERB_NOCONFIRMATION; NOPROGRESSUI must be 0x00000002 so bitwise combinations work correctly. Mark the enum with [Flags] to make combinations readable.
  • Use a single, shared P/Invoke/enum (not duplicated inside the form) to avoid maintenance issues.
  • pszRootPath == null empties the recycle bins on all drives for the calling user; pass a root (e.g. "C:\\") to target one drive.
  • Check the HRESULT return: 0 (S_OK) means success. Nonzero values indicate failures (locked files, permissions), so call Marshal.ThrowExceptionForHR or inspect the code to present a meaningful error.
  • Avoid emptying on Form.Load — doing it silently on startup surprises users. Trigger from an explicit action and consider leaving out SHERB_NOCONFIRMATION unless the UX requires silence.

Caveats:

  • Files in use or protected by permissions may not be removed; the API won’t override locks. For unattended automation, handle failures and log the HRESULT for diagnosis.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.