How Can I Delete All Files From a Folder Using a Macro?


How can i delete all the files from a folder using a macro.

The name of the folder is "HIDE PHOTOS DETA".

Start with this:

Delete All Files in Folder.kmmacros (2.8 KB)

If it doesn't work for you, we'll need more details about your workflow and goal. See Tip: How Do I Get The Best Answer in the Shortest Time? for best results.

Hey Greg,

What's the path of the folder?

Look at the Folder Contents collection.

** Note that the action will NOT delete folders (directories) – only files.


(This action will import directly into the macro you are currently working with in the Keyboard Maestro Editor.)

For Each.kmactions (952 B)

please i can not make the macro to delete the files
the files is on the desktop on the mac
please can you help me

In Chris' @ccstone macro, just enter this for the block below "The files in directory:"

OR, you could just click on the folder icon image and then choose your Desktop folder.

Hello @ccstone and @JMichaelTX

The macro launches the correct folder, but then I can't figure out how to tell the macro to find the folder contents and move to the trash. I've tried assigning the file path to the other sections of the macro that have a file path selection button (in red squares below), with and without tokens. How do I assign the folder contents to move to the trash?


Delete All Files in Folder.kmmacros (2.9 KB)

Hey Ernest,

You have. In the last For-Each action.

Look at my version with more readable variable names and an an extra test:

Delete All Files in Folder @ccstone v1.00.kmmacros (4.1 KB)

This works quite well for me on my system.

My only guess offhand about your problem is that you've accidentally assigned the wrong path to your Folder variable.

I recommend that you do not using single-word variables like Folder, as they are hard to read and distinguish from other terms in Keyboard Maestro.

I usually use lowerCamelCase for global variables to make sure I know know what my code is.

For local and instance variables I usually use this format local_CamelCase.

Let me know if the above macro works for you. If not we'll have to see if we can debug the problem.


Hello @ccstone,

Thanks for posting this. Your macro worked!

My goal is to move all folders out of a specific folder, in preparation for placing a single folder in that specific folder.

I simplified the macro down to just the actions I needed. I don't understand something though. In the "For Each Item in a Collection Execute Actions" action, the "For each" seems to designate the variable "Local_FilePath". I turned off every macro in KM except for this one. Yet it still worked. How is the "Local_FilePath" variable being defined? Is it saved in KM in the background?

Or is the "For Each" action defining the variable "Local_FilePath" for the next action? The "Move" action at the bottom?



It's intrinsic to the action:


Local_FilePath designates the current path in a list of paths, and that increments by 1 on each iteration of the For-Each.

For-Each is just a loop – and most loops act on an object.

Clear as mud?  :sunglasses: