.NET has no managed API for the Recycle Bin, so emptying it means calling SHEmptyRecycleBin in shell32.dll. One call clears the bin on every drive, or on a single drive if you pass its root path.
This is Windows-only. Target net8.0-windows, and enable unsafe code because [LibraryImport] generates it for you:
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
In a WinForms app add <UseWindowsForms>true</UseWindowsForms> as usual; nothing in the declaration below depends on it.
The declaration
using System.Runtime.InteropServices;
[Flags]
internal enum RecycleFlags : uint
{
None = 0,
NoConfirmation = 0x1, // SHERB_NOCONFIRMATION
NoProgressUI = 0x2, // SHERB_NOPROGRESSUI
NoSound = 0x4 // SHERB_NOSOUND
}
internal static partial class NativeMethods
{
[LibraryImport("shell32.dll", EntryPoint = "SHEmptyRecycleBinW",
StringMarshalling = StringMarshalling.Utf16)]
internal static partial int SHEmptyRecycleBin(
IntPtr hwnd, string? pszRootPath, RecycleFlags dwFlags);
}
The function returns an HRESULT, so the return type is int, not uint or bool. Zero means success and negative values are errors. Calling the W entry point directly makes sure paths are passed as UTF-16.
Empty it silently
const int E_UNEXPECTED = unchecked((int)0x8000FFFF);
int hr = NativeMethods.SHEmptyRecycleBin(
IntPtr.Zero,
null,
RecycleFlags.NoConfirmation | RecycleFlags.NoProgressUI | RecycleFlags.NoSound);
if (hr == 0)
{
Console.WriteLine("Recycle Bin emptied.");
}
else if (hr == E_UNEXPECTED)
{
Console.WriteLine("Nothing to delete.");
}
else
{
Marshal.ThrowExceptionForHR(hr);
}
Passing null as the root path empties the bin on all drives. To limit it to one drive, pass its root, for example @"C:\".
The E_UNEXPECTED branch is there because Windows typically returns that code when the bin is already empty. Treating it as a hard error makes your program fail on the most harmless case there is.
Let the user confirm
Without NoConfirmation, Windows shows its own “Are you sure you want to permanently delete these items?” dialog. In a WinForms app, pass the form’s handle so the dialog is owned by your window and stays on top of it:
private void EmptyBinButton_Click(object? sender, EventArgs e)
{
int hr = NativeMethods.SHEmptyRecycleBin(Handle, null, RecycleFlags.None);
if (hr < 0 && hr != unchecked((int)0x8000FFFF))
{
MessageBox.Show(this, $"Could not empty the Recycle Bin (0x{hr:X8}).");
}
}
I would keep the confirmation unless the user asked for silence in a setting. Emptying the bin is permanent, and the files belong to the user, not your application.
Things to keep in mind
The call only empties the bin for the current user. Running it elevated does not clear other users’ bins.
The call is synchronous and does not return until every file is deleted, which can take a while on a bin holding many thousands of files. Leave the progress UI on in that case so the user can see something is happening.
If what you actually want is to send a single file *to* the bin instead of deleting it, you don’t need P/Invoke. The Visual Basic file helpers are part of .NET and work from C#:
using Microsoft.VisualBasic.FileIO;
FileSystem.DeleteFile(
@"C:\Temp\old-report.pdf",
UIOption.OnlyErrorDialogs,
RecycleOption.SendToRecycleBin);
On .NET Framework
[LibraryImport] requires .NET 7 or later. On older runtimes the equivalent [DllImport] declaration is:
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);