How to clear recyclebin via command line?

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

+74
command-line windows windows-7 batch-file recycle-bin
Feb 11 '12 at 8:50
source share
11 answers

You can effectively empty the recycle bin from the command line by permanently deleting the recycle bin directory on the disk that contains system files. (In most cases, this will be the C: drive, but you should not specify this value because it will not always be true. Use the %systemdrive% environment variable %systemdrive% .)

The reason this tactic works is because each drive has a hidden protected folder called $Recycle.bin , where the $Recycle.bin bin actually stores deleted files and folders. When this directory is deleted, Windows will automatically create a new directory.

So, to delete a directory, use the rd (r emove d-directory) command with the /s option, which indicates that all files and directories in the specified directory should also be deleted:

 rd /s %systemdrive%\$Recycle.bin 

Please note that this action will permanently delete all files and folders in the recycle bin from all user accounts . In addition, you (obviously) will have to execute the command from an elevated command prompt in order to have sufficient privileges to perform this action.

+81
Feb 11 '12 at 9:30
source share

I prefer recycle.exe from Frank P. Westlake . It provides a pleasant status before and after. (I have been using special Frank utilities for 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 other uses and options (the output 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 /LC:\*.DOC Restore all DOC files which were recycled from any directory on the C: drive: RECYCLE /UC:\*.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" 
+24
Feb 28 '14 at 1:02
source share
+13
Jun 01 2018-12-12T00:
source share

You can use the powershell script (this works for users with folder redirection, and also so that their baskets do not take up space on the server)

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

The above script is taken here .

If you have Windows 10 and PowerShell 5, there is a Clear-RecycleBin .

To use the Clear-RecycleBin PowerShell insiders without confirmation, you can use the Clear-RecycleBin -Force . The official documentation can be found here.

+8
Mar 09 '16 at 15:57
source share

but

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

will delete the $ RECYCLE.BIN folder from the system drive, which is usually c :, you should delete it from any other accessible partitions, since there is a hidden $ RECYCLE.BIN folder in any partition on local and external drives (but not on removable drives, for example, on a USB drive that does not have the $ RECYCLE.BIN folder). For example, I installed the program in d: in order to delete the files that it moved to the Recycle Bin that I have to run:

rd / s / qd: \ $ RECYCLE.BIN

Additional information is available in Super User at Empty Trash from the command line.

+4
Dec 06 '14 at 3:24
source share

To stealthily delete everything, try:

 rd /s /q %systemdrive%\$Recycle.bin 
+3
Apr 13 '14 at 15:29
source share

I use these commands in a batch file to empty the trash:

 del /q /s %systemdrive%\$Recycle.bin\* for /d %%x in (%systemdrive%\$Recycle.bin\*) do @rd /s /q "%%x" 
+3
Sep 04 '16 at 5:47
source share

I know I'm a little late to the party, but I thought I could make my own subjective, more graceful option.

I was looking for a script that would empty the trash with an API call, instead of roughly deleting all files and folders from the file system. Having failed in my attempts to RecycleBinObject.InvokeVerb("Empty Recycle &Bin") (which, apparently, only works in XP or later), I came across discussions about using a function built into shell32.dll called SHEmptyRecycleBin() compiled language. I thought hey, I can do this in PowerShell and wrap it in a batch script script.

Save this with the .bat extension and run it to empty the trash. Run it with the /y switch to skip 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 complicated version that first calls SHQueryRecycleBin() to determine if bin is already empty before calling SHEmptyRecycleBin() . To do this, I got rid of the confirmation of choice and /y .

 <# : 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 
+3
Dec 17 '16 at 3:43
source share

I use this oneliner power shell:

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

Works for different drives except C :, too

+3
Mar 22 '17 at 7:30
source share

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 and then runs it.

+1
Jun 29 '18 at 8:26
source share

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

Here you are (from the cmd file):

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

And yes, it will be updated in Explorer.

0
Dec 10 '18 at 10:53
source share



All Articles