Cry How To...


Empty the recycle bin in a batch file or command window


You should only empty the recycle bin in a batch file if you have good reason to. The recycle bin is used by Windows Explorer and it will normally take care of removing old items from the recycle bin when it gets full.

At the command line

The recycle bin is a hidden folder in the root of each drive called $RECYCLE.BIN. It may be hidden but at the command prompt (or in a batch file) that will not stop you from cd'ing to the folder.

C:\> cd \$RECYCLE.BIN

 

C:\$RECYCLE.BIN>

If you do a simple "dir" it will show the folder as empty, but it probably isn't.

C:\$RECYCLE.BIN>dir

 Volume in drive C is System

 Volume Serial Number is 36FF-33C4

 

 Directory of C:\$RECYCLE.BIN

 

File Not Found

 

C:\$RECYCLE.BIN>

This is because the recycle bin holds folders and those folders are hidden. To get an idea of the number of folders in the recycle bin use:

attrib /s

This lists the attributes on everything in the folder and on everything in any folders it finds.

To delete everything in the folder the simplest way is simply to use "rmdir /s ." to recursively delete the contents.

C:\$RECYCLE.BIN>rmdir /s .

., Are you sure (Y.N)? y

The process cannot access the file because it is being used by another process.

 

C:\$RECYCLE.BIN>

The error "The process cannot access the file because it is being used by another process" simply means that the $RECYCLE.BIN cannot be deleted, and this is because it is the current working folder for your command session. This is a good thing, because we only want to clear out the recycle bin and not delete it.

You can confirm that the folder is now empty by again using "attrib /s"

C:\$RECYCLE.BIN>attrib /s

File not found - C:\$RECYCLE.BIN\*.*

 

C:\$RECYCLE.BIN>

In a batch script

If you are going to do this in a script then it is important to cd to the recycle bin to avoid deleting it. This is the script I use:

set Drive=C:

if exist %Drive%\$RECYCLE.BIN (
    pushd %Drive%\$RECYCLE.BIN
    del /s /q .
    popd
)


These notes have been tested with Windows 8.1 and may also apply to other versions of Windows.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.