Is There a "Clear All Variables" Command for KM?

I've been using KM for many years and only recently have I learned how incredibly powerful it can be to script and program all sorts of things.

I have a question— Is there a command to "clear all variables"?

I'm using various jumps to other macros, and I use a variable as a one time "flag" for a specific condition or state so another macro loop running asynchronously is aware of that state.

Do I need to explicitly null or re-zero these specific variables used as flags at the end of every macro? Or is there some hidden command to clear all local or global variables at the start or end or every macro? (I don't want/need to "delete" these variables. I just want to clear them or null them.) Thanks in advance.

Yes. These is no "clear all variables" KM Action.

Thank you for the response and the information. It would be nice to have such a command. I guess Peter has to balance power and being judicious about the number of commands KM offers.

There is no command built in to KM but you can roll your own, like this:

To clarify - this doesn’t delete variables, it just clears their contents.

If you don't need to "delete" variables then you really don't need a macro like this and, arguably, if you need to reset variables it might be best to create a macro that is specific to those variables alone and not, potentially, every variable in KM which is the danger with this macro if used carelessly.

Thank you for your response. Interesting approach. But I think I'm going to take your second suggestion- create a separate macro that I can call at the end of any macro that needs to reset the specific flag variables I use. I'm learning to think a bit more like a programmer.

1 Like

I only skimmed this thread, but do you know about Local and Instance variables? (See the Scope section.) This eliminates the need to clear out variables at the end of the macro.

1 Like

Dan, I'm glad you brought that up. I think you my have hit the nail on the head. I need to use local variables for flags. I've just become aware that they exist and I need to look at KM's documentation to change my flag variables (which I think are global) to local variables. Thank you.

Speaking of "local variables", having looked over the documentation, I'm wondering if the local variable would do it...I'm using the variable %Variable%flag% as a "flag" for an asynchronous second macro that does a repeat loop with a notification that it's running every 30 seconds. Would the "local variable" %Variable%local flag% be seen by the other macro running asynchronously??

No it wouldn’t and neither would an Instance variable unless the second macro is a sub-macro of the first.

OK, let's try this again, this time without accidentally posting the message before I finish. :roll_eyes:

@tiffle is right. For that situation, you definitely need normal variables. And kudos for using some complex logic - not everyone "gets" that kind of logic.

One thing you can do is use a naming convention with a specific prefix. Then you can delete variables with that prefix, using applescript, like this:


Here's the Applescript:

tell application "Keyboard Maestro Engine"
	set value of variables whose name starts with "gsmap" to "%Delete%"
end tell

Hey Dan,

I use this way of deleting variables I use in multiple macro's. Thank you for the solution. But for some reason the list of about 25 only deletes about 20 of the variables (all with the same prefix). No macro is running at time of deleting the variables except the macro for deleting the variables. Any idea or suggestion what is avoiding cleaning all the variables?


  • Neo

I don't know why this happens, but yes, it does happen. I just did a quick double-check and it still happens. Even if I do the equivalent in JXA instead of Applescript.

My only solution is to move to using Local and Instance variables everywhere. And in fact, I'm in the middle of working on that. It's a long process, which I'm doing litle by little.

And, in fact, that's one of the reasons I wrote my Variable Inspector Prompt macro. I can list all global variables, sort by size (if I want), and hunt them down in KM using one of the tool buttons in my Prompt.

If you decide to check it out, I'm about to upload version 2.0.2 which fixes a display/scroll bug when you have large dictionaries. But if that doesn't really affect you, then the current version 2.0.1 will work just fine.


Hey Dan,

Thank you for your quick reply and efforts also community wide as I've seen some of your impressive work. At this time I keep the KM preferences menu op to check and remove the remaining variables. I will check out your Variable Inspector Prompt.


  • Neo

If you're using KM10, you could try this way that makes use of the new %AccessedVariables% token:

It should get rid of any variables used in your macros regardless of their prefix.


Thanks @gglick for your suggestion, will check it out asap.


  • Neo

I thought I was the outlier. Glad to know it's not just me :sweat_smile:

1 Like

@gglick tried it but unfortunately it isn't doing anything with the variables I've set. I've tried the action also by itself but got the same out of it, nothing.

See the "edit" at the top of the post:

Bottom line: Don't use %AccessedVariables% to clean up variables.


Thank you, this was a lifesaver for me.

Screenshot 2024-02-05 at 20.30.12

Do i need to put the applescript inside this yellow box?

No. It's just for organization purposes.

1 Like