How to empty the Trash programmatically?

  • Hi,

    Is there a way to programmatically empty the Trash can?

    I could delete all files from /Users/<account>/.Trash folder, but I am
    not sure if it's a legitimate way. Also it will not include trashed
    files from other volumes, stored in /.Trashes/<account id>. The latter
    are generally not accessible even to list them without root
    permissions, so there is no easy way to empty that part of the Trash.

    Another related question would be how to measure the total size of all
    files in the Trash (other than scanning), including the files on other
    volumes.

    Thanks!
  • On Apr 15, 2010, at 11:05 PM, Oleg Krupnov wrote:

    > I could delete all files from /Users/<account>/.Trash folder, but I am
    > not sure if it's a legitimate way.

    It's not, because it will anger anybody who prefers to empty the trash "securely".
  • On Fri, Apr 16, 2010 at 1:05 AM, Oleg Krupnov <oleg.krupnov...> wrote:
    > Hi,
    >
    > Is there a way to programmatically empty the Trash can?

    This code is ancient, but it should give you an idea where to start
    writing a modern version:

    http://developer.apple.com/mac/library/samplecode/MoreAppleEvents/Listings/
    MoreFinderEvents_c.html


    In particular: MoreFEEmptyTrash().
  • On Apr 16, 2010, at 1:05 AM, Oleg Krupnov wrote:

    > Hi,
    >
    > Is there a way to programmatically empty the Trash can?
    >
    > I could delete all files from /Users/<account>/.Trash folder, but I am
    > not sure if it's a legitimate way. Also it will not include trashed
    > files from other volumes, stored in /.Trashes/<account id>. The latter
    > are generally not accessible even to list them without root
    > permissions, so there is no easy way to empty that part of the Trash.
    >
    > Another related question would be how to measure the total size of all
    > files in the Trash (other than scanning), including the files on other
    > volumes.
    >
    > Thanks!

    Why exactly do you want to do that?

    Charles
  • On Apr 15, 2010, at 11:05 PM, Oleg Krupnov wrote:

    > Is there a way to programmatically empty the Trash can?

    Send the Finder an ‘Empty Trash’ AppleEvent. You can use the scripting bridge API to do this pretty easily.

    —Jens
  • What is "securely" in this context?

    On Fri, Apr 16, 2010 at 9:14 AM, Dave Carrigan <dave...> wrote:
    >
    > On Apr 15, 2010, at 11:05 PM, Oleg Krupnov wrote:
    >
    >> I could delete all files from /Users/<account>/.Trash folder, but I am
    >> not sure if it's a legitimate way.
    >
    > It's not, because it will anger anybody who prefers to empty the trash "securely".
    >
    >
  • Yes, I see that I can use AppleScript. But is this really the only
    way? I would like to avoid Finder UIs popping up.

    On Fri, Apr 16, 2010 at 9:30 AM, Jens Alfke <jens...> wrote:
    >
    > On Apr 15, 2010, at 11:05 PM, Oleg Krupnov wrote:
    >
    >> Is there a way to programmatically empty the Trash can?
    >
    > Send the Finder an ‘Empty Trash’ AppleEvent. You can use the scripting bridge API to do this pretty easily.
    >
    > —Jens
  • tell application "Finder" to empty trash without warns before emptying
  • But the progress-bar window will still pop up...

    On Fri, Apr 16, 2010 at 9:41 AM, Dave Keck <davekeck...> wrote:
    > tell application "Finder" to empty trash without warns before emptying
    >
  • > But the progress-bar window will still pop up...

    I would consider that a part of the high-level concept of "emptying
    the trash." NSFileManager is probably what you're looking for, in
    combination with the mentioned AppleScript to update the trash icon:

        http://pastie.org/922743

    Needs error-checking, etc.
  • Forgive me for posting before reading all of your original message -
    having done so I see my response doesn't help much. Sorry for the
    noise.
  • Don't.

    The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.

    --Graham

    On 16/04/2010, at 4:45 PM, Oleg Krupnov wrote:

    > But the progress-bar window will still pop up...
    >
    > On Fri, Apr 16, 2010 at 9:41 AM, Dave Keck <davekeck...> wrote:
    >> tell application "Finder" to empty trash without warns before emptying
    >>
    >
  • On 16 Apr 2010, at 08:22, Graham Cox wrote:

    > Don't.
    >
    > The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.

    +1 to that.  Any application that emptied my Trash programmatically would find itself being the first new item in there.

    >
    > --Graham
    >
    >
    >
    > On 16/04/2010, at 4:45 PM, Oleg Krupnov wrote:
    >
    >> But the progress-bar window will still pop up...
    >>
    >> On Fri, Apr 16, 2010 at 9:41 AM, Dave Keck <davekeck...> wrote:
    >>> tell application "Finder" to empty trash without warns before emptying
    >>>
    >>

  • On 16 Apr 2010, at 09:46:27, Jeremy Pereira wrote:

    > On 16 Apr 2010, at 08:22, Graham Cox wrote:
    >
    >> Don't.
    >>
    >> The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.
    >
    > +1 to that.  Any application that emptied my Trash programmatically would find itself being the first new item in there.

    To be fair though, if it is a user-initiated emptying and it is done via Finder, then I think it is fine to initiate a Trash emptying from another app. What isn't fine is for it to be done behind the user's back.

    Matt Gough
  • On 16 Apr 2010, at 10:29, Matt Gough wrote:

    >
    > On 16 Apr 2010, at 09:46:27, Jeremy Pereira wrote:
    >
    >> On 16 Apr 2010, at 08:22, Graham Cox wrote:
    >>
    >>> Don't.
    >>>
    >>> The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.
    >>
    >> +1 to that.  Any application that emptied my Trash programmatically would find itself being the first new item in there.
    >
    > To be fair though, if it is a user-initiated emptying and it is done via Finder, then I think it is fine to initiate a Trash emptying from another app. What isn't fine is for it to be done behind the user's back.

    I'll concede that point but then why waste time duplicating a piece of functionality that is easily accessible from the dock?  Unless you are developing a dock/Finder replacement application.

    >
    > Matt Gough
  • On Apr 16, 2010, at 4:46 AM, Jeremy Pereira wrote:

    > +1 to that.  Any application that emptied my Trash programmatically would find itself being the first new item in there.

    ditto.

    <soapbox>
    The whole reason the trash exists is so users do not accidentally lose data. It gives them the opportunity to review the contents of the trash before emptying it and thereby allows them to retrieve files that have inadvertently been placed there (accidental overly broad Finder selection before dragging to the trash is a very common example). I know users who have been on the platform since the very first 128K Mac who quite deliberately only empty the trash once a week or so. When this feature first appeared with the original Mac it was a godsend for many because one now had a very easy, OS supported method of effectively undeleting files.

    Any app that empties the trash without the user's explicit consent is an unintended data loss waiting to happen.

    Don't do this. At least some of your users will be very unhappy. In extreme circumstances, you might even risk legal action because it could be argued that your app knowingly destroyed valuable user data without the user's knowledge or consent.
    </soapbox>

    Raffael Cavallaro
    <raffaelcavallaro...>
  • On Apr 15, 2010, at 11:45 PM, Oleg Krupnov wrote:

    > But the progress-bar window will still pop up…

    Every once in a while a question on cocoa-dev gives me the uncomfortable feeling that someone is trying to get help in writing a piece of malware. This is one of them. No offense if that’s not your actual goal, Oleg, but take it as a sign of how negatively people perceive this behavior.

    As others have said, if you give us more detail about what the actual purpose of this feature is, maybe we can suggest a better way to do it. Are you trying to make sure a particular file you created gets deleted securely? Emptying the trash isn’t the way to do it, then.

    —Jens
  • > The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.

    Graham, your point makes sense. I will think about it. In fact, I'm
    now researching the possibility, not already implementing it.

    > +1 to that.  Any application that emptied my Trash programmatically would find itself being the first new item in there.

    Jeremy, LOL :)

    > To be fair though, if it is a user-initiated emptying and it is done via Finder, then I think it is fine to initiate a Trash emptying from another app. What isn't fine is for it to be done behind the user's back.

    Matt, you're right, that's nearly how it would be implemented.

    > Every once in a while a question on cocoa-dev gives me the uncomfortable feeling that someone is trying to get help in writing a piece of malware. This is one of them. No offense if that’s not your actual goal, Oleg, but take it as a sign of how negatively people perceive this behavior.
    >
    > As others have said, if you give us more detail about what the actual purpose of this feature is, maybe we can suggest a better way to do it. Are you trying to make sure a particular file you created gets deleted securely? Emptying the trash isn’t the way to do it, then.
    >

    No, Jens, your feeling is wrong. I'm developing an app for disk space
    management, particularly for freeing up disk space. Actually you can
    take a look: http://www.daisydiskapp.com. The new version of the app I
    am developing will allow deleting files, and (maybe) emptying the
    trash as well. So, it would be just a user-initiated command, a
    shortcut to what Finder does. I am actually surprised my question
    could raise any suspicion. Why? That's amusing... :)
  • On Apr 16, 2010, at 9:07 AM, Oleg Krupnov wrote:

    > No, Jens, your feeling is wrong. I'm developing an app for disk space
    > management, particularly for freeing up disk space. Actually you can
    > take a look: http://www.daisydiskapp.com. The new version of the app I
    > am developing will allow deleting files, and (maybe) emptying the
    > trash as well. So, it would be just a user-initiated command, a
    > shortcut to what Finder does.

    That sounds fine, sorry for casting suspicion on your work :) Sending an event to the Finder is definitely the way to go.

    > I am actually surprised my question
    > could raise any suspicion. Why? That's amusing... :)

    Well, we all know the Mac OS has a huge malware problem, so we have to be paranoid.

    But to be serious, I think it’s always a good idea when you ask a question to give a little bit of context about the app feature that’s driving your development. It helps people give you better answers.

    —Jens
  • On Apr 16, 2010, at 12:22 AM, Graham Cox wrote:

    > Don't.
    >
    > The only app that the user wants to empty the trash is Finder, not yours. If they want it emptied (or emptied securely), they'll go to the Finder and use the menu there. It's OK for your app to move stuff to the trash as long as it's clear to the user that will happen, but emptying it is not your business. The clue really is the fact that doing so is not supported directly by an API.

    Hey all of you of limited vision standing on your soapboxes - give me a break!  He couldn't possibly be asking because he wants to create a Finder/desktop replacement app (like the Finder works so well we don't need one of those).  These kinds of preachy know-it-all rants are really tiresome, but unfortunately they happen a lot on this list.

    __jayson

    Circus Ponies NoteBook - Organization for a Creative Mind
    www.circusponies.com
  • On Fri, Apr 16, 2010 at 9:30 AM, Jayson Adams <jayson...> wrote:
    > Hey all of you of limited vision standing on your soapboxes - give me a break!  He couldn't possibly be asking because he wants to create a Finder/desktop replacement app (like the Finder works so well we don't need one of those).  These kinds of preachy know-it-all rants are really tiresome, but unfortunately they happen a lot on this list.

    They happen a lot on this list because that kind of attention is what
    sets the Mac development community apart from that of other operating
    systems. Developers—especially the kind who take the time to hang out
    here—really care about the overall user experience and quality of the
    platform. When people come asking for help with things that could
    break the paradigm, we respond accordingly, so as to protect the
    deserved reputation of the platform as a whole.

    --Kyle Sluder
  • On Apr 16, 2010, at 12:30 PM, Jayson Adams wrote:

    > He couldn't possibly be asking because he wants to create a Finder/desktop replacement app (like the Finder works so well we don't need one of those).

    Before replacing the Finder with a "superior" implementation one might want to be aware of what the design goals, expected behavior, user expectations, and existing capabilities of the "inferior" existing Finder actually are.

    The OP apparently isn't even aware of the Finder's "Secure Empty Trash" capability:

    On Apr 16, 2010, at 2:34 AM, Oleg Krupnov wrote:

    > What is "securely" in this context?

    He tells us that it's for a disk cleaning utility. But emptying the trash will delete files on *other mounted volumes* as well, not just the disk being examined.

    One shouldn't violate well established, expected platform behavior with the possible, unlikely exception of  those who are expertly knowledgeable about that well established, expected behavior. That doesn't appear to be the case here.

    warmest regards,

    Ralph

    Raffael Cavallaro
    <raffaelcavallaro...>
  • On Apr 16, 2010, at 9:46 AM, Kyle Sluder wrote:

    > On Fri, Apr 16, 2010 at 9:30 AM, Jayson Adams <jayson...> wrote:
    >>
    >> These kinds of preachy know-it-all rants are really tiresome, but unfortunately they happen a lot on this list.
    >
    > They happen a lot on this list because that kind of attention is what
    > sets the Mac development community apart from that of other operating
    > systems. Developers—especially the kind who take the time to hang out
    > here—really care about the overall user experience and quality of the
    > platform. When people come asking for help with things that could
    > break the paradigm, we respond accordingly, so as to protect the
    > deserved reputation of the platform as a whole.

    Preachy, know-it-all rants are never an appropriate response.  I do already understand how the people who do this kind of thing justify it as being OK.

    __jayson

    Circus Ponies NoteBook - Organization for a Creative Mind
    www.circusponies.com
  • > But emptying the trash will delete files on *other mounted volumes* as well, not just the disk being examined.

    rm -rf ~/.Trash/*

    For volumes other than the startup volume it is a little more complicated:

    rm -rf /Volumes/volume_name/.Trashes/uid/*

    The Finder seems to notice when you do this and keeps in sync.

    Looking in these folders will also allow you to give the user some idea of what he is about to permanently delete.

    Paul Sanders.
  • On Apr 16, 2010, at 1:43 PM, Paul Sanders wrote:

    >> But emptying the trash will delete files on *other mounted volumes* as well, not just the disk being examined.
    >
    > rm -rf ~/.Trash/*
    >
    > For volumes other than the startup volume it is a little more complicated:
    >
    > rm -rf /Volumes/volume_name/.Trashes/uid/*
    >
    > The Finder seems to notice when you do this and keeps in sync.
    >
    > Looking in these folders will also allow you to give the user some idea of what he is about to permanently delete.

    Yes, I'm fully aware of what's going on when this happens. That's not the point. An application that purports to free disk space on a specific volume under examination should not be deleting user files on other volumes! He *wants* to empty the trash on *all* mounted volumes, possibly because he wants to erase the trash on a specific mounted volume under examination, not just ~/.Trash/. This is a blunderbuss approach with a high likelihood of data loss:

    User accidentally drags file A-file on volume A to trash with other files user did intend to trash. User finds that volume B is near full and examines it with OP's app. OP's app empties trash, deleting A-file, even though this frees zero additional space on volume-B. Oops!

    Again, someone who doesn't have an expert knowledge of the intended operation of the Finder and an equally experienced knowledge of user expectations shouldn't be doing this.

    warmest regards,

    Ralph

    Raffael Cavallaro
    <raffaelcavallaro...>
  • On Apr 16, 2010, at 10:43 AM, Paul Sanders wrote:

    > rm -rf ~/.Trash/*
    >
    > For volumes other than the startup volume it is a little more
    > complicated:
    >
    > rm -rf /Volumes/volume_name/.Trashes/uid/*

    Please don't hardcode those paths. Use system APIs like FindFolder
    instead to locate the trash directory for a given volume. Or better
    yet, ask the Finder to do it, so the user gets progress information
    and feedback about errors.

    —Jens
  • > Please don't hardcode those paths. Use system APIs like
    > FindFolder
    > instead to locate the trash directory for a given volume.

    Well, OK, but you get the idea.

    Paul Sanders.
  • On Apr 16, 2010, at 11:03, Raffael Cavallaro wrote:

    > Again, someone who doesn't have an expert knowledge of the intended operation of the Finder and an equally experienced knowledge of user expectations shouldn't be doing this.

    In the [possibly] few moments that remain before this thread is moderated out of existence, I'd like to add that I have *some* sympathy for Jayson's perspective.

    We keep shooting ourselves in the foot on this list by not first asking enough questions. Remember the "thou shalt not forcibly turn the system alert on and to maximum volume" thread last year? It turned out to be about hurricane warnings, and the good of mankind generally, and puppies. (No, sorry, not the puppies.)

    In the current case, I was guessing that the trash-emptying was something to do with cleaning up in a school lab after classes were over. In such a case, forcibly emptying the trash might be a good thing (though the need to do it without user interface would be a bit puzzling).

    It turns out that wasn't a correct guess, and we're actually talking about disk space recovery. Yet, useful as the concept a disk-space-recovery application may be to some types of users, the concept of *reinforcing* a user's ignorance (not intended pejoratively) by making a 3rd-party app his/her *usual* way of emptying the trash seems a bit misguided.

    Perhaps an application like this *should* invoke the Finder, openly and even brazenly ("Now asking the Finder to empty the Trash for you, so that you can follow along and see that no puppies are harmed in the process...") in the hope of gently re-training the user to, you know, get into the habit of emptying the trash without assistance.

    And now, back to our regularly scheduled flame wars ...
  • On 16.04.10 11:29, Matt Gough wrote:
    > To be fair though, if it is a user-initiated emptying and it is done via Finder, then I think it is fine to initiate a Trash emptying from another app. What isn't fine is for it to be done behind the user's back.

    As long as it is obvious that this empties the system-wide trash. After
    all, Mail.app and other programs have their own trashes, and you
    wouldn't want a user to think they're emptying a local trash and
    suddenly delete a file they meant to recover.

    -- Uli
    http://www.zathras.de
  • > As long as it is obvious that this empties the system-wide trash. After
    > all, Mail.app and other programs have their own trashes, and you
    > wouldn't want a user to think they're emptying a local trash and
    > suddenly delete a file they meant to recover.

    In case it wasn't obvious, my earlier "rm -rf" post was intended
    to show how to empty the trash for a particular volume only.  I
    believe that might be what the OP was looking for.

    Paul Sanders.
  • People,

    This my first post on this list, and I am very disappointed that I am chiming into this kind of conversation. This will be my only post on this subject and I think I can make myself very clear.

    On 16/04/2010, at 14:22, Jayson Adams wrote:

    > On Apr 16, 2010, at 9:46 AM, Kyle Sluder wrote:
    >
    >> On Fri, Apr 16, 2010 at 9:30 AM, Jayson Adams <jayson...> wrote:
    >>>
    >>> These kinds of preachy know-it-all rants are really tiresome, but unfortunately they happen a lot on this list.
    >>
    >> They happen a lot on this list because that kind of attention is what
    >> sets the Mac development community apart from that of other operating
    >> systems. Developers—especially the kind who take the time to hang out
    >> here—really care about the overall user experience and quality of the
    >> platform. When people come asking for help with things that could
    >> break the paradigm, we respond accordingly, so as to protect the
    >> deserved reputation of the platform as a whole.
    >
    > Preachy, know-it-all rants are never an appropriate response.  I do already understand how the people who do this kind of thing justify it as being OK.

    I don't think ranting is a good counter-attack on ranting. I mean, if you don't like the attitude, don't act in the same manner.

    Sure, assuming that one is writing malware, just because he/she wants to empty the trash programatically, is kind of stupid and paranoid.

    But I agree with Kyle when he talks about "the kind [of people] who take the time to hang out here". People have no obligation to answer questions. And people asking questions should be clear, because time is valuable for everybody. Period.

    Cheers,
    Flavio
  • On 17/04/2010, at 4:41 AM, Quincey Morris wrote:

    > We keep shooting ourselves in the foot on this list by not first asking enough questions.

    Why should we though? If a poster can't muster the effort to sketch an outline of why they want to do a certain thing, it falls into the category of not asking a smart question in the first place. I'm happy to answer questions where I can but playing a game of 20 questions to fill in all the blanks that should have been there in the original post is something I just don't have time for.

    If the original post lacks context, then the context applied by default is a standard app that has to live with everything else on a system where the user is in charge. If the question was about writing a Finder replacement (where certain privileges would be elevated) then it should have said so.

    In the default context, I don't believe what I said was a "preachy, know-it-all rant", and certainly wasn't intended that way. You don't have to read this list for very long to see just how much baggage people bring from other platforms. 24 years of Mac programming experience (and making similar mistakes as well) must be worth something, surely?

    --Graham
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today