deleting empty folders

some workflows involve creating standard sets of job folders at the beginning of a project and then going through and cleaning out any unused folders at the end. this is a good way to standardise archiving procedures but it can also be tedious to clean up once the project is done. this post shows how to easily delete empty folders using applescript.

thanks to xander at cadcoder (sorry this link seems to be broken at the moment — Aug 2013) for the basic structure of the shell command.

Applescript Icon to follow along, copy and paste the script examples into applescript editor (found in applications > utilities) or, for older OS versions, script editor (applications > applescript). we’re going to set this one up as a droplet (so you’ll save it from script editor as an application). you can save a droplet to your desktop or drag it to your sidebar for easy access. to run the script just drag and drop a folder onto the droplet’s icon.

the basic form of the script goes like this :

on open mgItem
  do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -type d -empty -delete;"
end open

in the shell, a folder is known as a directory (d). the first part of the command changes the current directory (cd) to our dropped folder. then it performs a search of that folder (find) for empty subfolders (-type d -empty) and deletes them. simple.

this works a treat — most of the time. but macs have these dastardly hidden files, the most common of which are the good old .DS_Store files which help the finder do its thing. a folder containing nothing but a .DS_Store file looks empty, but as far as the shell is concerned, it’s not.

so, here’s a variation to deal with that issue — first delete any .DS_Store files, then delete empty folders :

on open mgItem
  do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;"
end open

ok, that’s better. but to make the droplet a little more user-friendly, we should ensure it can handle multiple dropped folders — rather than having to drop them one at a time :

on open mgItems
  repeat with mgItem in mgItems
    do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;"
  end repeat
end open

and to make it more robust, it should not have a conniption if we inadvertently drop files instead of, or as well as, folders :

on open mgItems
  repeat with mgItem in mgItems
    set mgItem to mgItem as alias
    tell application "System Events" to set theClass to get class of item (mgItem as string)
    
    if (theClass as string) contains "cfol" then
      do shell script "cd " & quoted form of POSIX path of mgItem & " && find . -name .DS_Store -delete; find . -type d -empty -delete;"
    end if
    
  end repeat
end open

that’s about it — super fast, super easy way to clean out any unused folders.

for those interested in this sort of thing…
applescript droplet icon when an applescript is saved as a droplet, its icon includes a downwards pointing arrow. the on open mgItems line at the start of this script is what allows the saved application to run as a droplet. without that on open command you’d need to find some other way to reference the folders to be processed. renaming finder items shows a couple of other ways you could do this.

macgrunt icon

5 thoughts on “deleting empty folders

  1. This works well on MacOs Sierra when changing this line:
    if (theClass as string) contains “cfol” then

    to this:
    if (theClass as string) contains “fol” then

  2. Hi

    I’d like make this work as a folder script. We have like a dump area on the server. I am adding a folder script so when new items arrive it will auto delete items older than a year and then remove any empty folders after that.

    How can I make your script understand to use the same folder?

    My script starts like this:

    on adding folder items to this_folder after receiving added_items
    try

    So how can I make your script understand the “this_folder” part? It doesn’t have to a be dynamical thing (should the folder move). I could use a fixed path to this folder but I still can’t connect the dots here.

    • G’day Max

      I haven’t worked a lot with folder actions but I’ll try to help however I can.

      You should be able to do it by taking all the elements between ‘repeat…’ and ‘end repeat’ and putting them after your code that deletes old items.
      And then just change all instances of mgItems to this_folder (that’s mgItems with the ‘s’ — leave references to mgItem untouched)

      on adding folder items to this_folder after receiving added_items
      try
      [your code here]
      end try
      repeat with mgItem in this_folder
      ..... etc ...
      end repeat
      end adding folder items to

      let me know how you go
      m.

  3. Pingback: Batch Commands That Gets The Jobs Done « …..troubledblogger's blog

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s