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?
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 (remove directory) 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.
$Recycle.bin
, Recycled
, or Recycler
? Is there a variable to do that, or is the only way via catching the exceptions? –
Interloper /q
switch so rd
doesn't give you an extra prompt. rd /s /q %SYSTEMDRIVE%\$Recycle.bin
–
Myriapod cmd /c "rd /s %systemdrive%\$Recycle.bin"
–
Rationality 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"
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
but I haven't found it. So, please, if you have a built-in way, please share it! –
Inveteracy 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 @Romilda described. This solution does not. –
Fedora 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
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
Clear-RecycleBin
successfully deleted some malformed folder names in recycle bin that I could not remove otherwise. –
Lasalle You can use this PowerShell command.
Clear-RecycleBin -Force
Note: If you want a confirmation prompt, remove the -Force flag
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
–
Galling I use this powershell oneliner:
gci C:\`$recycle.bin -force | remove-item -recurse -force
Works for different drives than C:, too
To stealthily remove everything, try :
rd /s /q %systemdrive%\$Recycle.bin
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 -- https://mcmap.net/q/87613/-how-to-empty-recyclebin-through-command-prompt
@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 -- https://mcmap.net/q/87613/-how-to-empty-recyclebin-through-command-prompt
@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
iex
-ing your idea? (btw it is possible to embed C# code in batch file with msbuild inline tasks without need of compiling exe) –
Veronaveronese 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. –
Inconsequent 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
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"
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.
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
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"
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. –
Galling 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.
© 2022 - 2024 — McMap. All rights reserved.
$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. – Romilda