7

I need a code to empty the recycling bin without conformation I have tried the simple del $Recycle.Bin but it says access denied even when elevated does any one know a code I could use.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
09stephenb
  • 9,358
  • 15
  • 53
  • 91

5 Answers5

2

This emptied my bin without any confirmation.

@ECHO OFF
start /b /wait powershell.exe -command "$Shell = New-Object -ComObject Shell.Application;$RecycleBin = $Shell.Namespace(0xA);$RecycleBin.Items() | foreach{Remove-Item $_.Path -Recurse -Confirm:$false}"
Knuckle-Dragger
  • 6,644
  • 4
  • 26
  • 41
  • but maybe is permissions issue if this fails. Are you trying to delete your own recycle bin or one owned by another user ? – Knuckle-Dragger Jan 14 '14 at 11:08
  • All of them from all accounts – 09stephenb Jan 14 '14 at 13:15
  • I have tried it and it says $Shell is not a valid program or command – 09stephenb Jan 15 '14 at 10:03
  • forgot to add the start wrapper to run powershell from batch. – Knuckle-Dragger Jan 15 '14 at 10:18
  • The powershell command doesn't empty the recycle bin through a system command, but just deletes the files individually, right? – foxidrive Jan 15 '14 at 10:22
  • Yep, it loops through them one by one. – Knuckle-Dragger Jan 15 '14 at 10:29
  • `C:\$Recycle.Bin\S-1-5-blah\$R4JAJOK.pdf: You do not have sufficient access rights to perform this operation.` <-- I get this error on 4 files with a `read only` attribute. There are a whole load of files left in the folder which are all 544 bytes in size - JPG DOC TXT etc. – foxidrive Jan 15 '14 at 10:54
  • Can you delete it after taking ownership ? – Knuckle-Dragger Jan 16 '14 at 06:35
  • I just saw your reply (no @foxidrive there). If a script has to take ownership then it would also need admin permissions, but it was just the read-only attribute that was different. I'm not sure what the 544 byte files were designed to do - the binary content looked like a link to a folder. – foxidrive Jan 23 '14 at 07:35
2

Above answers are ok for cmd batch files but for the new powershell there is a better way

Simply use the cmdlet

Clear-RecycleBin

Optionally you can use the -Force or -Confirm:$false parameters so it won't ask for confirmation

For more info open powershell and type

Get-Help Clear-RecycleBin

DGaleano
  • 137
  • 9
1

I have just found this.

erase /s/q/f "C:\$RECYCLE.BIN\*">nul
09stephenb
  • 9,358
  • 15
  • 53
  • 91
1

Guaranteed to delete all content in the Recycle Bin for the selected drive while leaving the folder itself intact:

C:\$Recycle Bin\>rd . /q /s
  • Change to the required drive
  • Change into the $Recycle Bin folder
  • Run the command rd . /q /s [remove-dir (currentdir) /quiet /subdir]

You will get an error that the current directory is still in use (because that is your current location) and can't be deleted. This is expected behaviour because I want the $Recycle Bin folder to remain.

Altered-Ego
  • 436
  • 5
  • 6
0

I thought the /s parameter had to come before the /q:
rd /s /q C:\$Recycle.Bin should be the command to run and empty recycle bin from a batch file, right?

Stephan
  • 53,940
  • 10
  • 58
  • 91