Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Empty the recycle bin from the command line

Post image for Empty the recycle bin from the command line

by Steve Wiseman on February 22, 2012 · 20 comments

in Tips,Tools,Utility


.

Anglelika had a question a few weeks back:

“Mr Steve. I need to clear the recycle bin when users log out. Is there any way I can put a command in their logout script to do this? PS. Don’t ask why…just something the boss wants : ()”

This is an interesting one. I thought a quick and simple VB Script solution would work.

It turns out you need to parse out the name of the Recycle bin. Doing it like this can break, since the Recycle bin might be renamed to something different – or it might be another language version of Windows.

Plus, from with some of my testing, I noticed that sometimes the VB Script would prompt the user – something we definitely don’t want from a command line script.

Instead, I created a very simple command line application that can do this for you. It is called EmptyRecycle.exe

It clocks just under 100K, so this guy is really small.

To use it, make sure your Recycle bin is loaded with junk:

Recycle Bin

Nahh..Just kidding about that. Your recycle bin can be empty.

Just call it from the command line:

Empty Recycle Example

And within a few moments, your recycle bin will be empty.

Get it from our download page:
http://www.intelliadmin.com/index.php/downloads/

As always, no spyware, or adware. Just freeware goodness.

It is free to use for Personal, and Commercial use.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 20 comments… read them below or add one }

1 David Chang February 22, 2012 at 2:05 pm

Steve,

I was just looking for something like this on a public workstation…users want their recycle to work, so we did not want to disable it. This is a good solution

2 X February 22, 2012 at 3:00 pm

What about using CCleaner command line, like in:
%ProgramFile%\ccleaner.exe /AUTO [/SHUTDOWN]
So Anglelika’s users will even keep their nose clean…

P.S.: Change %ProgramFiles% for whatever suits your OS fancy.

3 Mark S February 22, 2012 at 3:36 pm

Hey Mr X,

Doesn’t that require you install CCleaner on the users system?

I like this solution since I don’t have to install anything 🙂

4 Steve Wiseman February 22, 2012 at 3:37 pm

Hello X,

I will have to check it out. Did not know you could do this with CCleaner.

Is CCleaner free for commercial use?

5 X February 22, 2012 at 5:12 pm

Taken from their site at http://www.piriform.com/ccleaner/faq/general/can-i-use-ccleaner-in-a-business-or-corporate-network:
“Can I use CCleaner in a Business or Corporate Network?
Yes, CCleaner is already used by many large and small companies all over the world. Including many government and military establishments.
We have a range of Business packages and titles, more details can be found in out Business Section.”

CCleaner doesn’t need to be installed: You can just copy the files into a folder.
You can update your logoff script and ‘install’ CCleaner in the same (script) shot.

6 Steve Wiseman February 22, 2012 at 10:16 pm

X,

Awesome. Thanks for the tip. Will definitely check it out. 🙂

7 Angus Scott-Fleming February 23, 2012 at 9:57 am

WRT to CCleaner, you can use the portable version so you do need to install it, you just need to unZip it somewhere shared.

Here’s a batch file which, when run with admin rights, empties not just the current user’s Recycle Bin but ALL the recycle bins on a system:

————————————
ATTRIB %systemdrive%\RECYCLER\* -R -S -H /S /D
DEL %systemdrive%\RECYCLER\* /F /S /Q
RD %systemdrive%\RECYCLER /S /Q
ATTRIB D:\RECYCLER\* -R -S -H /S /D
DEL D:\RECYCLER\* /F /S /Q
RD D:\RECYCLER /S /Q
PAUSE
————————————

I include drive D: as my system has two drives, C: and D:, and each has its own portion of the RB.

8 Steve Wiseman February 24, 2012 at 1:08 am

Hello Angus,

Great info. Thanks for taking the time to post it for everyone.

Steve

9 Wyatt February 29, 2012 at 2:06 pm

FYI: CCleaners proper name is Crap Cleaner. 🙂

10 Robert March 3, 2012 at 6:11 pm

Hello Agnus,

Thanks for the listing. Is this command line going to remove a RECYCLER directory as well? Is RECYCLER the recycle bin itself?

RD %systemdrive%\RECYCLER /S

11 Angus S-F March 4, 2012 at 11:59 pm

@Robert March: Windows XP recreates the RECYCLER folder automagically.

12 Carol February 28, 2013 at 3:36 pm

RD %systemdrive%\RECYCLER /S

RD or DIR both claim that folder doesn’t exist. Did you try it with Windows XP?

13 Jonny V May 24, 2013 at 1:31 pm

I used this with Speech recognition to create a macro that allows me to empty my recycle bin. I also have one that hibernates my system. Is there a switch to make it run “hidden’? do I use “hh.exe” in the command line? I’ve seen that used before to run a program in hidden mode.

14 Steve Wiseman May 24, 2013 at 1:43 pm

There is not much we can do since it is a command line program. Is there any option in your speech recognition software to do this?

15 Gerry March 11, 2015 at 12:12 pm

Running Windows 2008 R2 SP1. I cant get your EmptyRecycle.exe to run through a scheduled task. have you ever tried this?

16 Steve Wiseman March 11, 2015 at 12:16 pm

Hello Gerry,

This does make some sense. How do you have the user account setup for it? The empty recycle program requires that the user is logged in, and it has to run under their account. The reason for this is that it is calling an explorer shell api. If EmptyRecycle.exe is run under the system account it probably will silently fail.

17 Gerry March 11, 2015 at 12:33 pm

Steve:

YES, that is exactly what is happening. The scheduled task is created as a system task. My idea was to have your RecycleBin.exe run once a night to remove anything that engineers deleted when in there remotely, at the site the servers are not logged on.

18 Steve Wiseman March 11, 2015 at 12:59 pm

Hello Gerry,

Just playing around here on a windows 8 system…I think this is the same on other versions of windows.

One alternative is a bat file that looks like this:

del c:\$Recycle.Bin *.* /s /q

$Recycle.bin is a hidden folder where all the users recycle bins are stored. The subfolders are a SID of the user…this will clear them all

To see those folders you need to do a dir /a:h

Try it on a test system before doing this of course…have not played enough to determine if this breaks something.

If it works, it will definitely work under the system account

19 Gerry March 11, 2015 at 2:45 pm

Steve:

This was interesting. I tried this first:

c:\>del c:\$Recycle.bin *.* just to see what it would do manually, it showed the following:

could not find c:\$Recycle.bin\*
c:\*.* Are you Sure (Y/N)?

I can see the Recycle bin folder in windows explorer and when I do dir /a:h

Weird.

20 Steve Wiseman March 11, 2015 at 4:43 pm

Not sure if the website is removing formatting…but it looks like your syntax is wrong.

You would want to delete like this:

del c:\$Recycle.bin\*.*

Not

del c:\$Recycle.bin *.*

The second one will delete everything in recycle…plus your current folder!

Leave a Comment

Category Links - Windows Forum - Exchange Forum