none
Empty Recycle Bin for all users

    Question

  • With a VBS script I want to empty Recycle Bin for all users.
    I know how to do it with my user, but how to do it for the other users?
    Wednesday, October 21, 2009 11:53 AM

Answers

  • If you search the VBScript newsgroup archive you can find a script that looks like this:


     
    Const CLSID = "::{645FF040-5081-101B-9F08-00AA002F954E}"
    
    Set oShell = CreateObject("Shell.Application")
     
    Set oRecycleBin = oShell.Namespace(0).ParseName(CLSID) 
    oRecycleBin.InvokeVerb "Empty Recycle &Bin"
    


    It uses Shell.Application object to empty recycle bin for the current user. Depending on the user settings, it is possible that the confirmation dialoq will be shown asking the user to confirm file deletion. It also displays a progress popup window. Also, depending on user regional settings, it is possible that the verb to empty recycle bin will not be named 'Empty Recycle Bin'.

    Another way to do this is to use Shell.Application to get the path for each deleted file or folder, and then use FSO to delete them:


    const ssfBITBUCKET = 10
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    
    Set objShell = CreateObject("Shell.Application")
    Set objBitBucket = objShell.Namespace(ssfBITBUCKET)
    Set colDeleted = objBitBucket.Items()
    
    For Each objDeleted In colDeleted
        If objDeleted.Type = "File Folder" Then
            objFso.DeleteFolder(objDeleted.Path)
        Else
            objFso.DeleteFile(objDeleted.Path)
        End If
    Next
    


    This script will not show any dialogs, but when the files are deleted, the Recycle Bin icon on the desktop will not change to empty, and the script also works only for the current user.

    On my computer (Windows XP SP 3) Recycle Bin folder paths look like this:

    DriveLetter:\RECYCLER\User Account SID\

    You could try to use this to delete the files in each user's Recycle Bin. You would first need to enumerate the users (using the Win32_UserAccount WMI class) and use their SIDs to construct path to each user's Recycle Bin folder. Then you would use Cim_DataFile to enumerate the files in each Recycle Bin folder and delete them. This is what the script could look like:


    Const strRecycler = "\\RECYCLER\\"
    
    Set objSWbemServices = GetObject _
        ("WinMgmts:Root\Cimv2")
    
    Set colUsers = objSWbemServices.ExecQuery _
        ("Select * From Win32_UserAccount")
    
    For Each objUser In colUsers
    
        Set colDisks = objSWbemServices.ExecQuery _
            ("Select * From Win32_LogicalDisk " & _
            "Where DriveType = 3")
    
        For Each objDisk In colDisks
    
            Set colDeletedFiles = objSWbemServices.ExecQuery _
            ("Select * From Cim_DataFile Where Drive = '" _
            & objDisk.DeviceId _
            & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
            & "And Hidden = False")
    
            For Each objDeletedFile In colDeletedFiles
                WScript.Echo objDeletedFile.Name
                'objDeletedFile.Delete
            Next
            
            Set colDeletedFolders = objSWbemServices.ExecQuery _
            ("Select * From Win32_Directory Where Drive = '" _
            & objDisk.DeviceId _
            & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
            & "And Hidden = False")
    
            For Each objDeletedFolder In colDeletedFolders
                WScript.Echo objDeletedFolder.Name
                'objDeletedFolder.Delete
            Next
    
        Next
    
    Next
    

    I haven't used this kind of script before, and I don't know of any issues you might find, so if you decide to go this way make sure you do enough testing. I also don't know if it will work on Windows versions other than XP SP3.



    Uros Calakovic
    Wednesday, October 21, 2009 3:41 PM
    Moderator
  • Thank you very much, it works!

    Win32_UserAccount returns all the domain users and is quite an overhead.
    I switched to the get profile list from the registry
    (Source: http://blogs.technet.com/heyscriptingguy/archive/2005/06/03/hey-scripting-guy-how-can-i-list-all-the-user-profiles-on-a-computer.aspx)

    Regards
    Leandro
    • Marked as answer by LeandroZ Thursday, October 22, 2009 7:23 AM
    Thursday, October 22, 2009 7:22 AM

All replies

  • If you search the VBScript newsgroup archive you can find a script that looks like this:


     
    Const CLSID = "::{645FF040-5081-101B-9F08-00AA002F954E}"
    
    Set oShell = CreateObject("Shell.Application")
     
    Set oRecycleBin = oShell.Namespace(0).ParseName(CLSID) 
    oRecycleBin.InvokeVerb "Empty Recycle &Bin"
    


    It uses Shell.Application object to empty recycle bin for the current user. Depending on the user settings, it is possible that the confirmation dialoq will be shown asking the user to confirm file deletion. It also displays a progress popup window. Also, depending on user regional settings, it is possible that the verb to empty recycle bin will not be named 'Empty Recycle Bin'.

    Another way to do this is to use Shell.Application to get the path for each deleted file or folder, and then use FSO to delete them:


    const ssfBITBUCKET = 10
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    
    Set objShell = CreateObject("Shell.Application")
    Set objBitBucket = objShell.Namespace(ssfBITBUCKET)
    Set colDeleted = objBitBucket.Items()
    
    For Each objDeleted In colDeleted
        If objDeleted.Type = "File Folder" Then
            objFso.DeleteFolder(objDeleted.Path)
        Else
            objFso.DeleteFile(objDeleted.Path)
        End If
    Next
    


    This script will not show any dialogs, but when the files are deleted, the Recycle Bin icon on the desktop will not change to empty, and the script also works only for the current user.

    On my computer (Windows XP SP 3) Recycle Bin folder paths look like this:

    DriveLetter:\RECYCLER\User Account SID\

    You could try to use this to delete the files in each user's Recycle Bin. You would first need to enumerate the users (using the Win32_UserAccount WMI class) and use their SIDs to construct path to each user's Recycle Bin folder. Then you would use Cim_DataFile to enumerate the files in each Recycle Bin folder and delete them. This is what the script could look like:


    Const strRecycler = "\\RECYCLER\\"
    
    Set objSWbemServices = GetObject _
        ("WinMgmts:Root\Cimv2")
    
    Set colUsers = objSWbemServices.ExecQuery _
        ("Select * From Win32_UserAccount")
    
    For Each objUser In colUsers
    
        Set colDisks = objSWbemServices.ExecQuery _
            ("Select * From Win32_LogicalDisk " & _
            "Where DriveType = 3")
    
        For Each objDisk In colDisks
    
            Set colDeletedFiles = objSWbemServices.ExecQuery _
            ("Select * From Cim_DataFile Where Drive = '" _
            & objDisk.DeviceId _
            & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
            & "And Hidden = False")
    
            For Each objDeletedFile In colDeletedFiles
                WScript.Echo objDeletedFile.Name
                'objDeletedFile.Delete
            Next
            
            Set colDeletedFolders = objSWbemServices.ExecQuery _
            ("Select * From Win32_Directory Where Drive = '" _
            & objDisk.DeviceId _
            & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
            & "And Hidden = False")
    
            For Each objDeletedFolder In colDeletedFolders
                WScript.Echo objDeletedFolder.Name
                'objDeletedFolder.Delete
            Next
    
        Next
    
    Next
    

    I haven't used this kind of script before, and I don't know of any issues you might find, so if you decide to go this way make sure you do enough testing. I also don't know if it will work on Windows versions other than XP SP3.



    Uros Calakovic
    Wednesday, October 21, 2009 3:41 PM
    Moderator
  • Thank you very much, it works!

    Win32_UserAccount returns all the domain users and is quite an overhead.
    I switched to the get profile list from the registry
    (Source: http://blogs.technet.com/heyscriptingguy/archive/2005/06/03/hey-scripting-guy-how-can-i-list-all-the-user-profiles-on-a-computer.aspx)

    Regards
    Leandro
    • Marked as answer by LeandroZ Thursday, October 22, 2009 7:23 AM
    Thursday, October 22, 2009 7:22 AM
  • It doesn't have to be that complex.  I use WMI to grab all local drives from a machine, check for a "recycle bin" folder, then clear the contents of the folder (which empties all user's bins).  If they send something to the recycle bin Windows will just recreate their folder.  I've been doing this for years with no problems.

    I found this thread searching for WMI and "Recycle Bin" to see if I can pull the default location of the bins because 2008/Vista/7 names it differently.  I have a script to clear junk off of servers and need to revamp it for Vista/2008/7.  I'd post it but my company would be very unhappy with me.

    Having said that, if you're already grabbing user account from AD you can check profile folders and see if the user account still exists...if it doesn't you can delete it to free space.

    My script does the following (remotely):
    -clears recycle bins on all local drives
    -removes profile folders for terminated users (I cheat and use a database with employee accounts/status, instead of using AD)
    -removes old patch uninstall folders in \Windows that are older than X days
    -removes .dmp files older than X days
    -clears the temp folders provided the files are not in use (I wimped out and attempt to delete them all and ignore 'access denied' errors)

    It's scheduled and does quite a bit more (auditing, checking for certain services, AV, blah blah) but this thread is space oriented.

    All of that is easy to do, it just takes some work and imagination.  Again, sorry i can't post my script but hopefully someone else will see this and say "hey man, that's easy, i can do it"
    Sunday, February 14, 2010 8:18 AM
  • A simpler solution would be to use the rd (rmdir) command to remove the RECYCLER subfolders. For example,
    rd /s C:\recyler
    Tuesday, August 17, 2010 7:41 PM
  • A simpler solution would be to use the rd (rmdir) command to remove the RECYCLER subfolders. For example,
    rd /s C:\recyler
    C:\>rmdir /?
    Removes (deletes) a directory.
    RMDIR [/S] [/Q] [drive:]path
    RD [/S] [/Q] [drive:]path
        /S      Removes all directories and files in the specified directory
                in addition to the directory itself.  Used to remove a director
                tree.
        /Q      Quiet mode, do not ask if ok to remove a directory tree with /S
    C:\>
    For anyone reading this, to avoid any mistakes, rmdir also removes the directory itself, not just the subfolders. 

    Andreas Hultgren
    MCTS, MCITP
    http://ahultgren.blogspot.com/
    Friday, September 30, 2011 7:17 AM
  • A simpler solution would be to use the rd (rmdir) command to remove the RECYCLER subfolders. For example,
    rd /s C:\recyler

    How do you find the recyler to remove it?

     It is not always in the same place.  It may exist at the root of a drive but can be moved in Terminal Server and in Win7/Vista.


    jv
    Friday, September 30, 2011 7:24 AM
  • In general, I think it is a bad idea to remove a system directory (even if you know its location and have permissions to remove it) if you are not sure of the implications. Removing the directory means its permissions are lost, as A.Hultgren pointed out.

    There's no API to "delete all users' recycle bin data" (at least, not one that I know about), and I suspect the reason is that this is considered the users' territory.

    That said, however, Marty List wrote EmptyRecycleBin.exe that uses the correct API (the SHEmptyRecycleBin function) which can run silently. You could put this in a logon script, but I don't recommend it, as I would find it rather rude if my files got deleted without my permission.

    HTH,

    Bill

    Friday, September 30, 2011 2:32 PM
    Moderator