How can I tell that a directory is really a recycle bin?

Here's a question inspired by an actual customer question:

I need a function that, given a path, tells me whether it is a Recycle Bin folder. I tried using functions like SHGetSpecialFolderPath with CSIDL_BITBUCKET, but that doesn't work because the Recycle Bin is a virtual folder that is the union of the Recycle Bins of all drives.

The customer noted that they don't want to hard-code the words RECYCLED and RECYCLER, which is a good decision because the name of the directory depends on many things. I mentioned earlier that it depends on the file system. It also depends on whether the drive is accessed locally or remotely; network-based Recycle Bin folders follow yet another naming scheme. It may even depend on what operating system the user is running. No, hard-coding the name of the Recycle Bin folders is not a good idea.

The SHDESCRIPTIONID structure tells you a little more about a shell folder. In addition to the "description ID", it also gives you a CLSID, and it is the CLSID that is relevant here.

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>
#include <stdio.h>

GetFolderDescriptionId(LPCWSTR pszPath, SHDESCRIPTIONID *pdid)
  if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL,
                                        &pidl, 0, NULL))) {
    IShellFolder *psf;
    LPCITEMIDLIST pidlChild;
    if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder,
                                    (void**)&psf, &pidlChild))) {
      hr = SHGetDataFromIDList(psf, pidlChild,
                     SHGDFIL_DESCRIPTIONID, pdid, sizeof(*pdid));
  return hr;

int __cdecl wmain(int argc, WCHAR **argv)
  if (SUCCEEDED(GetFolderDescriptionId(argv[1], &did)) &&
      did.clsid == CLSID_RecycleBin) {
    printf("is a recycle bin\n");
  } else {
    printf("is not a recycle bin\n");
  return 0;

The GetFolderDescriptionId function takes the path to a folder and converts it to an ITEMIDLIST just so it can call SHGetDataFromIDList to obtain the SHDESCRIPTIONID. All we care about in this case is whether the CLSID is the Recycle Bin or not.

C:\> checkrecycle C:\Windows
is not a recycle bin
C:\> checkrecycle C:\RECYCLER\S-1-5-21-2127521184-1604012920-1887927527-72713
is a recycle bin

Of course, now that I told you how to do it, I have to tell you that this is not how to do it. This is another example of a customer having a problem, solving half of it, and asking for help with the other half, unaware that they are approaching the problem from the wrong direction. Next time, we'll look at the customer's real problem.

Comments (20)
  1. Pierre B. says:

    Is there an equivalent way to tell if the current wallpaper is a funny wallpaper?

  2. John says:

    Pierre: No, but it doesn’t matter since none of the links actually take you to wallpaper of any kind.

  3. Adam says:

    I’m guessing that the customer’s real problem is that they’re doing something like trying to prevent a file from being moved to (or from) the recycle bin.

  4. Name required says:

    They were probably trying to move the * domain into the recylce bin.

  5. Dave says:

    How can I tell that a directory is really a recycle bin? Duh. The only files in it will be deleted files.

  6. Erzengel says:

    Guys, if you respond to the pingbacks to random pages, Raymond won’t be able to delete it without deleting your comments (or leaving your comments dangling making future readers wonder why TF you’re talking about wallpapers).

    Anyway, I’m wondering if they’re trying to "undo" or something. I’ve wanted to programatically undo a move to the Recycle Bin, but Google is not your friend in this case.

  7. Mark says:

    This story, especially coupled with Erzengel’s comments, is starting to scare me.

    I’m envisioning one of "those" companies, you know the kind, that change file associations without permission, use direct registry edits to bypass driver validation warnings, or have uninstallers that leave items on your hard drive. This company wants a scheduled task or service that checks your recycling bin for the "leftovers" their uninstaller doesn’t delete, and if it finds them, restores them “for you”.

    Maybe it’s just a failure of imagination on my part, but I’m having a hard time coming up with a good reason for programmatically undoing a move to the recycling bin.

  8. Dan says:

    My guess is the customer wants to send files to the recycle bin.

    uTorrent, Visual Studio, etc do this when they "delete" files on request by the user.

    I know there’s gotta be two ways to do this, but only because I’ve seen software do them:

    You can invoke the Windows "Are you sure you want to recycle/delete this file?" dialog.  This gives you the added bonus of having shift depressed automatically skipping the recycle bin.  Keyboard shortcut consistency ftw.

    You can move files to the recycle bin without a prompt.  I imagine you simply get the PIDL of the (desktop) recycle bin and call whatever shell folder API function to move files there.  (I dunno much about shell object programming :( )

  9. John says:

    If you want to send stuff to the recycle bin you would just use SHFileOperation().  I suspect the customer wants to do something far more sinister; my guess is it involves some kind of hook to prevent something from being moved to the recycle bin.

  10. peted says:

    "This is another example of a customer having a problem, solving half of it, and asking for help with the other half, unaware that they are approaching the problem from the wrong direction."

    In fact, one of the things I try to convey to every person who ever asks me a question about how to do something is that programming should almost always be easy. If you find yourself trying to do something, and are discovering that the platform (be it the OS, a framework, the language, etc.) seems to be getting in your way, that’s almost always a sure sign that you’re "approaching the problem from the wrong direction".

    There are exceptions, to be sure. But they are few and far between, much more rare than most people believe. A programmer’s first thought upon running into this kind of difficulty should be "maybe there’s a different, better way to accomplish my goal".

  11. Gabe says:

    It’s not too hard to imagine some sort of desktop search app that wants to crawl the HD, but doesn’t want to index files in the Recycle Bin.

    I’m sure there are other good reasons to want to do this. For example, if you wanted to calculate how much free space was available on a disk, you might want to add in the space used by "deleted" files.

  12. Mark says:

    Thank you, Gabe.

    You are absolutely correct- I was approaching the problem from the wrong direction. There are plenty of reasons why you wouldn’t want to perform an action on the recycling bin. I was only considering operations that would happen if the folder was the recycling bin.

    I’ll be able to sleep tonight now, thanks to you. Or maybe this afternoon, if my user requirements or product specifications don’t show up in my e-mail soon….

  13. DrkMatter says:

    "Maybe it’s just a failure of imagination on my part, but I’m having a hard time coming up with a good reason for programmatically undoing a move to the recycling bin."

    The key word here is "undo". For example, imagine working in a software that gives you a view of certain files as part of a project. When you hit the delete key, it sends one of those files in the recycle bin. If, after that, you select "Undo" from the menu, the file is restored.

  14. Joe Butler says:

    There are lots of recycle bins, and they still don’t know which one is the current user’s bin?

  15. Jules says:

    Joe Butler: Indeed.  And there’d be no guarantee they’d even find the right recycle bin directory without doing a traversal of the entire directory structure of the machine (I presume that if you’ve mounted a volume in an empty ntfs directory it still gets its own recycle bin as a subdirectory of that directory… never tried, but that’d be the only way that makes sense), let alone any obvious way of identifying which was the right one.

  16. Name required says:

    Raymond won’t be able to delete it

    If you’d pointed this out before I would have sold the spammers my services ;)

  17. Mark says:


    You’ve just described a very different scenerio- IE, I want to find the recycling bin, so that I can perform a function on it.

    While I guess you could enumerate the entire directory structure and check each directory indvidually to see if it is the recycling bin, that doesn’t strike me as the optimal solution. Of course, we already know that whatever the customer was doing, it’s not the Optimal solution, otherwise we wouldn’t be making fun of… I mean, learning from this customer.

    In that scenerio, I think you’d probably be better off looking up the location of the Recycling bin, and then using that. It’s been a long time, but I think SHGetFolderLocation(CSIDL_BITBUCKET) used to work for this. Note that this may have been deprecated- It’s been a long time since I’ve done anything in this area. I’m fairly sure there is a new way to do this under Vista, although knowing Microsoft the old way probably still works, it just might get you made fun of, perhaps on this blog.

    As you can tell, I haven’t done a lot of work with recycling bins under Vista, or in a long time. As such, any information above may not be correct, and should not be used in a production enviroment. :-)

  18. Erzengel says:

    DrkMatter is correct, I want to be able to let the user select "delete" on a file, which sends it to the recycle bin (already done that).

    However, right now that particular step is removed from the Undo history (It says "You cannot undo this" when you select delete because, like I said, Google is not your friend in this case).

    So Mark, you jumped to the wrong conclusion, and I take exception to your associating me with malware companies. Your "failure of imagination" was more like a "failure of reading comprehension".

    See, when I googled for "undoing move to recycle bin" (this was some time ago) I got suggestions that included one much like what the customer’s stated request: iterate the directory structure, find the recycle bin, find the file you recycled, and move it. (And, of course, the classic "Search for the string "Recycle" and open that folder")

    That is why I wondered if that was the case here. Obviously from the next article we know it wasn’t.

    Mostly I don’t work with Windows API, I work with the PS3. Windows API is useful only when I’m making tools for creating the assets, so it’s entirely likely there’s ways that I just can’t figure out, and I perhaps don’t know the magic incantation to get Google to tell me. These days I use it even less, only when the .NET API lets me down (like moving to recycle bin and back again)

  19. Mark says:

    Diplomacy isn’t my forte. I was actually referring to several legitimate vendors, if you search this blog, you’ll find the references. It was clear that Mr. Chen would prefer these names be omitted. Perhaps I should have said "undoing a move to the recycling bin in this manner, as this method reminds me of similiar hacks used by some companies to change file associations…".

    I can’t speak about several years ago, but today-

    First result is this article. Third is a MSDN article from 2001.

    If I may, how did you put the file into the recycling bin? At what point, if any, did you think "Searching the HDD and testing each directory individually can’t be the right way- After all, I knew where the recycling bin was when I put the file there, so the data has to be stored somewhere"? Was this code ever released commercially, or was this a personal project? Did QA kick up a red flag the first time they tried it on a Network drive, or even on a machine with 2 HDD’s? Or even 1 large HDD?

  20. Erzengel says:

    Umm, ShFileOperation, with FO_DELETE and FOF_ALLOWUNDO. I don’t actually do any moving here, I specifically say to the OS "Hey, delete this file. But allow it to undoable." The OS is the one actually moving the file about, my program has no clue where the recycle bin is.

    And yes, I noticed that today there seems to be articles that show what should be done. Any new tools will include that in the history, and I’ll revise any that are still in use. Maybe.

    Like I said, I work on the PS3. Anything with my name in it has only appeared commercially on the PS3. The I use the Windows SDK in the tools made to create assets. As the people using such tools are colleages and can be told "If you want the file back, it’s in the recycle bin. Sorry, the program can’t undo it itself.", it’s not that big of an issue.

    As I said, I use the OS’s delete function to "move to the recycle bin". Network drives, multiple hard drives, etc, all function correctly. It’s trying to undo it that is difficult because there is no "Hey, you know that undoable delete I just told you to do? Yeah, can you tell me where the file is so I can undo it?" Instead there’s a rather more convoluted method including the use of a virtual folder and iterating it for the file I just deleted, at the time I deleted it, and hopefully it will be there. Will that work on network drives and multiple hard drives? I don’t know, I haven’t actually tested it yet. I do know that my deleting the file does work, though.

Comments are closed.

Skip to main content