100

Usually we delete the recycle bin contents by right-clicking it with the mouse and selecting "Empty Recycle Bin". But I have a requirement where I need to delete the recycle bin contents using the command prompt. Is this possible? If so, how can I achieve it?

0

14 Answers 14

112

You can effectively "empty" the Recycle Bin from the command line by permanently deleting the Recycle Bin directory on the drive that contains the system files. (In most cases, this will be the C: drive, but you shouldn't hardcode that value because it won't always be true. Instead, use the %systemdrive% environment variable.)

The reason that this tactic works is because each drive has a hidden, protected folder with the name $Recycle.bin, which is where the Recycle Bin actually stores the deleted files and folders. When this directory is deleted, Windows automatically creates a new directory.

So, to remove the directory, use the rd command (r​emove d​irectory) with the /s parameter, which indicates that all of the files and directories within the specified directory should be removed as well:

rd /s %systemdrive%\$Recycle.bin

Do note that this action will permanently delete all files and folders currently in the Recycle Bin from all user accounts. Additionally, you will (obviously) have to run the command from an elevated command prompt in order to have sufficient privileges to perform this action.

Sign up to request clarification or add additional context in comments.

7 Comments

A few more caveats: the change in bin status may not reflect in Explorer (the desktop icon) until you actually open the Recycle Bin and/or refresh the desktop, it only affects that particular volume; recycled files on other drives will not be affected, so you may not be actually emptying the recycle bin with this method, and the directory name can vary by Windows version (and I believe filesystem as well). It may be $Recycle.bin, Recycled, Recycler, etc. and you may even have more than one if you multi-boot—programs like Norton Recovery Bin have their own directories.
@Synetech, How do we figure out is it $Recycle.bin, Recycled, or Recycler? Is there a variable to do that, or is the only way via catching the exceptions?
Note: If you're using this in a script, you will also want to pass in a /q switch so rd doesn't give you an extra prompt. rd /s /q %SYSTEMDRIVE%\$Recycle.bin
Does not work on my Windows 10. Nothing happens after using this command.
Reminder this only works in CMD. If you want it to run in powershell, try cmd /c "rd /s %systemdrive%\$Recycle.bin"
|
35

I prefer recycle.exe from Frank P. Westlake. It provides a nice before and after status. (I've been using Frank's various utilities for well over ten years..)

C:\> recycle.exe /E /F
Recycle Bin: ALL
    Recycle Bin C:  44 items, 42,613,970 bytes.
    Recycle Bin D:   0 items, 0 bytes.
            Total:  44 items, 42,613,970 bytes.

Emptying Recycle Bin: ALL
    Recycle Bin C:   0 items, 0 bytes.
    Recycle Bin D:   0 items, 0 bytes.
            Total:   0 items, 0 bytes.

It also has many more uses and options (output listed is from /?).

Recycle all files and folders in C:\TEMP:
  RECYCLE C:\TEMP\*

List all DOC files which were recycled from any directory on the C: drive:
  RECYCLE /L C:\*.DOC

Restore all DOC files which were recycled from any directory on the C: drive:
  RECYCLE /U C:\*.DOC

Restore C:\temp\junk.txt to C:\docs\resume.txt:
  RECYCLE /U "C:\temp\junk.txt" "C:\docs\resume.txt"

Rename in place C:\etc\config.cfg to C:\archive\config.2007.cfg:
  RECYCLE /R "C:\etc\config.cfg" "C:\archive\config.2007.cfg"

6 Comments

Doesn't this have the same problem Synetech mentioned? stackoverflow.com/questions/9238953/…
@Pacerier I don't show the Recycle Bin on my desktop, so I never noticed before whether the icon updates or not. After just testing it, the icon is updated correctly after emptying via recycle.exe. As far as the directories are concerned, I expect that it is using a Win32 API to empty the Recycle Bin. Having said that, I have used this util for many years on Windows including XP, 7, 8, 8.1 and Server 2003, 2012, 2012 R2. (I probably used it on Vista and Server 2008, but didn't run those OSes very long, so I can't say for certain..)
Yea, it's not a particularly useful tool, especially since there are in-built ways to do it already.
@Pacerier built in ways!? Like navigating Windows Explorer with the keyboard and/or mouse? Using the icon on the desktop (assuming the recycle bin is even displayed on the desktop)? Neither of which solve the op's question. I really do think there is a command somewhere for emptying the bin, like deleting history RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1 but I haven't found it. So, please, if you have a built-in way, please share it!
@Pacerier: First you suggest this solution has flaws like the RD /S [/Q] solution - which @wasatchwizard refuted by testing - then the exact same RD /S [/Q] solution suddenly is superior, because it's "built-in". How does that make sense? RD /S [/Q] has the problems @Synetech described. This solution does not.
|
17

You can use this PowerShell command.

Clear-RecycleBin -Force

Note: If you want a confirmation prompt, remove the -Force flag

3 Comments

Works, the gives error: Clear-RecycleBin : The system cannot find the path specified At line:1 char:1 + Clear-RecycleBin -Force + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception + FullyQualifiedErrorId : FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand
This should be the first thing people try.
This emptys the current user's recycle bin. Not the whole recycle bin.
16

nircmd lets you do that by typing

nircmd.exe emptybin

http://www.nirsoft.net/utils/nircmd-x64.zip
http://www.nirsoft.net/utils/nircmd.html

2 Comments

thanks i needed that when i had alot in my recycle and windows clicks to empty dose not work thanks.
@shareef Thank you for the comment :)
14

You can use a powershell script (this works for users with folder redirection as well to not have their recycle bins take up server storage space)

$Shell = New-Object -ComObject Shell.Application
$RecBin = $Shell.Namespace(0xA)
$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}

The above script is taken from here.

If you have windows 10 and powershell 5 there is the Clear-RecycleBin commandlet.

To use Clear-RecycleBin inside PowerShell without confirmation, you can use Clear-RecycleBin -Force. Official documentation can be found here

2 Comments

Clear RecycleBin is useful in PowerShell in windows 10
Clear-RecycleBin successfully deleted some malformed folder names in recycle bin that I could not remove otherwise.
6

I use this powershell oneliner:

gci C:\`$recycle.bin -force | remove-item -recurse -force

Works for different drives than C:, too

Comments

5

To stealthily remove everything, try :

rd /s /q %systemdrive%\$Recycle.bin

Comments

5

I know I'm a little late to the party, but I thought I might contribute my subjectively more graceful solution.

I was looking for a script that would empty the Recycle Bin with an API call, rather than crudely deleting all files and folders from the filesystem. Having failed in my attempts to RecycleBinObject.InvokeVerb("Empty Recycle &Bin") (which apparently only works in XP or older), I stumbled upon discussions of using a function embedded in shell32.dll called SHEmptyRecycleBin() from a compiled language. I thought, hey, I can do that in PowerShell and wrap it in a batch script hybrid.

Save this with a .bat extension and run it to empty your Recycle Bin. Run it with a /y switch to skip the confirmation.

<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264

@echo off & setlocal

if /i "%~1"=="/y" goto empty

choice /n /m "Are you sure you want to empty the Recycle Bin? [y/n] "
if not errorlevel 2 goto empty
goto :EOF

:empty
powershell -noprofile "iex (${%~f0} | out-string)" && (
    echo Recycle Bin successfully emptied.
)
goto :EOF

: end batch / begin PowerShell chimera #>
Add-Type shell32 @'
    [DllImport("shell32.dll")]
    public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
        int dwFlags);
'@ -Namespace System

$SHERB_NOCONFIRMATION = 0x1
$SHERB_NOPROGRESSUI = 0x2
$SHERB_NOSOUND = 0x4

$dwFlags = $SHERB_NOCONFIRMATION
$res = [shell32]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $dwFlags)

if ($res) { "Error 0x{0:x8}: {1}" -f $res,`
    (New-Object ComponentModel.Win32Exception($res)).Message }
exit $res

Here's a more complex version which first invokes SHQueryRecycleBin() to determine whether the bin is already empty prior to invoking SHEmptyRecycleBin(). For this one, I got rid of the choice confirmation and /y switch.

<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264

@echo off & setlocal
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

: end batch / begin PowerShell chimera #>
Add-Type @'

using System;
using System.Runtime.InteropServices;

namespace shell32 {
    public struct SHQUERYRBINFO {
        public Int32 cbSize; public UInt64 i64Size; public UInt64 i64NumItems;
    };

    public static class dll {
        [DllImport("shell32.dll")]
        public static extern int SHQueryRecycleBin(string pszRootPath,
            out SHQUERYRBINFO pSHQueryRBInfo);

        [DllImport("shell32.dll")]
        public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
            int dwFlags);
    }
}
'@

$rb = new-object shell32.SHQUERYRBINFO

# for Win 10 / PowerShell v5
try { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb) }
# for Win 7 / PowerShell v2
catch { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb.GetType()) }

[void][shell32.dll]::SHQueryRecycleBin($null, [ref]$rb)
"Current size of Recycle Bin: {0:N0} bytes" -f $rb.i64Size
"Recycle Bin contains {0:N0} item{1}." -f $rb.i64NumItems, ("s" * ($rb.i64NumItems -ne 1))

if (-not $rb.i64NumItems) { exit 0 }

$dwFlags = @{
    "SHERB_NOCONFIRMATION" = 0x1
    "SHERB_NOPROGRESSUI" = 0x2
    "SHERB_NOSOUND" = 0x4
}
$flags = $dwFlags.SHERB_NOCONFIRMATION
$res = [shell32.dll]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $flags)

if ($res) { 
    write-host -f yellow ("Error 0x{0:x8}: {1}" -f $res,`
        (New-Object ComponentModel.Win32Exception($res)).Message)
} else {
    write-host "Recycle Bin successfully emptied." -f green
}
exit $res

4 Comments

Very nice improvement of batch/powershell chimera technique (+1). Is the iex-ing your idea? (btw it is possible to embed C# code in batch file with msbuild inline tasks without need of compiling exe)
@npocmaka Thanks! I think many others have used iex this way. I forget where I picked up that ${%~f0} is equivalent to gc "%~f0". It was this cat who turned me onto out-string. Can you link an example of embedding C# in a batch script with msbuild? My Google-fu is weak today.
here - dostips.com/forum/… - though you cannot pass command line arguments directly - you'll need to define a variable with the arguments that later can be extracted from the c# part
and in the example above .net version is not parametrized.
4

while

rd /s /q %systemdrive%\$RECYCLE.BIN

will delete the $RECYCLE.BIN folder from the system drive, which is usually c:, one should consider deleting it from any other available partitions since there's an hidden $RECYCLE.BIN folder in any partition in local and external drives (but not in removable drives, like USB flash drive, which don't have a $RECYCLE.BIN folder). For example, I installed a program in d:, in order to delete the files it moved to the Recycle Bin I should run:

rd /s /q d:\$RECYCLE.BIN

More information available at Super User at Empty recycling bin from command line

1 Comment

Ahhhh I've been trying to figure out why the suggested commands above didn't work. Specifying f: for mine made it work. Thanks a lot! <3
3

i use these commands in a batch file to empty recycle bin:

del /q /s %systemdrive%\$Recycle.bin\*
for /d %%x in (%systemdrive%\$Recycle.bin\*) do @rd /s /q "%%x"

2 Comments

Win 10 doesn't have a %systemdrive%\$Recycle.bin directory
@Zimba of course it doesn't. everyone here is just making things up that sound cool
2

Yes, you can Make a Batch file with the following code:

cd \Desktop

echo $Shell = New-Object -ComObject Shell.Application >>FILENAME.ps1
echo $RecBin = $Shell.Namespace(0xA) >>FILENAME.ps1
echo $RecBin.Items() ^| %%{Remove-Item $_.Path -Recurse -Confirm:$false} >>FILENAME.ps1


REM The actual lines being writen are right, exept for the last one, the actual thigs being writen are "$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}"   
But since | and % screw things up, i had to make some changes.

Powershell.exe -executionpolicy remotesigned -File  C:\Desktop\FILENAME.ps1

This basically creates a powershell script that empties the trash in the \Desktop directory, then runs it.

Comments

2

I use EmptyRecycleBin.py python script

You will need to pip install winshell

#!python3

# Empty Windows Recycle Bin

import winshell

try:
    winshell.recycle_bin().empty(confirm=False, show_progress=True, sound=False)
    print("Recycle Bin emptied")

except:
    print('Recycle Bin is already empty')

You can change the Boolean False and True statements to either turn on or off the following: Confirm yes\no dialog, progress bar, sound effect.

If you don't use python, this one-liner for powershell is great.

I actually have it in EmptyRecycleBin.ps1, and use it in Git Bash.

Clear-RecycleBin -Force

Comments

1

Create cmd file with line:

for %%p in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%p:\$Recycle.Bin" rundll32.exe advpack.dll,DelNodeRunDLL32 "%%p:\$Recycle.Bin"

4 Comments

Win 10 doesn't have $Recycle.Bin in drive root
Just checked Windows 10 Pro build 2004 and it does. Maybe you should enable option of showing hidden/system files and folders in your file manager.
Aha! Found it: Command still works, but file wasn't visible in explorer; I'd already selected Show Hidden files, and wasn't visible in C: drive. It does become visible when I uncheck Hide protected operating system files (separate option). Cheers.
rundll32.exe advpack.dll,DelNodeRunDLL32 doesn't empty bin for me: Win 10.0.17134. Fixed: works in Elevated prompt.
0

All of the answers are way too complicated. OP requested a way to do this from CMD.

Here you go (from cmd file):

powershell.exe /c "$(New-Object -ComObject Shell.Application).NameSpace(0xA).Items() | %%{Remove-Item $_.Path -Recurse -Confirm:$false"

And yes, it will update in explorer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.