About Resetting Variables

Is it effective when I use random clicks? Doesn't it make sense to do that?
When would you use this action?


1 Like

Bit unclear what you're asking.

Do you want to delete variables? If so, you can use a Set Variable to Text action, setting the variable to %Delete%. However, you should also learn about using local variables. They do not need to be deleted, as they only exist while a macro runs. So, as an example, instead of using MyVar, use Local__MyVar.



First off I don't understand Keyboard Maestro any more than you think. I had no idea what this script meant. But when I read some forum it was attached with some macro. I thought I needed it and continued to use it. So I posted it here to clarify what it is. There are probably several such macros. I'm in the process of sorting it out now.

Thank you for letting me know :pray:

1 Like

I’d go one step further and say there’s only one case when you might actually need to delete a variable - and it only ever applies to global variables. A global variable keeps the value you’ve set it to, even when you reboot your Mac; so if you’ve put some “sensitive” information into a global variable (such as a bank account number for example) it would be sensible to delete that variable after you’ve finished using it just to make sure that nobody else can see that information should you leave your Mac available to strangers.

Of course, @noisneil is absolutely correct in advising that you should try to use local and/or instance variables in preference to global variables, since those two types of variable are automatically deleted by KM once the macro they are used in finishes running.

I know it can be a bit confusing having these different types of variable, but it is worth studying the KM wiki here; manual:Variables [Keyboard Maestro Wiki]


Thank you for your message. Your specific example is very clear. And I was able to understand I have to study both English and keyboard maestro. That's why I'm having such a hard time. But when you mix in such a nice example, I can quickly see why it's necessary. that's a great explanation!!!

I understand that KM deletes automatically. @noisneil is my respected teacher who always helps me.
But you also thank you for providing me with a really great opinion. I always appreciate the people here on the forums :pray:


To expound on the good advice @noisneil and @tiffle have shared, I use global variables extensively while creating, modifying or debugging a macro.

For instance, when creating a new macro, I use global variables and prepend them with debug__, that way if I see them in the variable pane in the KM preferences window, I know they are for a macro I am currently working on. But, since they are global (I.e. persistent in that they don’t go away and their content does not change unless one of my actions changes it), I may need them to be “reset” at the beginning of each macro execution. For example, if the macro I am building appends text to a variable, then I likely need that variable reset before each execution that way it doesn’t get superfluous text in it. For this case, I use the following AppleScript at the beginning of the macro to first set the content of all variables that begin with debug__ to “BLANK”, and then I delete them.

AppleScript to empty and delete all debug__ variables (click to expand/collapse)
set varName1 to "debug__"
set varAction1 to "BLANK"
set varAction2 to "%Delete%"

tell application "Keyboard Maestro Engine"
	set value of variables whose name starts with varName1 to varAction1
	set value of variables whose name starts with varName1 to varAction2
end tell

AppleScript explanation:

I use variables (such as varName1 and varAction1) to simplify certain things.
varName1 is how the variable is prepended.
varAction1 is to set those variable to “BLANK” first.
varAction2 is to then delete those variables.

so the line: set value of variables whose name starts with varName1 to varAction1 is basically telling Keyboard Maestro Engine to set all variables that start with debug__ to “BLANK”, and the next line is telling it to delete them.

Why set them to “BLANK” first? Because I have found that if a global variable does not contain any text, then AppleScript does not actually delete it. The why of this is beyond both my comprehension and my desire to understand it. :sweat_smile: Suffice it to say, I just don’t care, as long as it goes away and no longer clutters up my variables list.

To wrap this up, once I have finished building, modifying or debugging the macro in question, I have a macro that automatically converts all debug__ variables into local__ variables that way I don’t have to worry about it anymore.

If this doesn’t make sense, or you want to see some examples, feel free to reach out and I’d be happy to explain further.



Hi, @igh_033. @noisneil, @tiffle, and @cdthomer have already shared lots of great information, but I thought I'd weigh in too...

You specifically asked about:

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

I've not seen the issue the @cdthomer mentioned, but there's a lot I've not seen :grinning:. So based on his experience, the above should probably be rewritten:

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

If you had two variables named gsmap_1 and gsmap_2 (global variables because they do not have a local or instance prefix) you could use the above AppleScript to delete them or you could use the Set Variable to Text action twice:

Keyboard Maestro Export

I've placed them in a Group action and renamed the group (Delete global variables) to provide some simple documentation.

So back to the AppleScript. If I had many global variables with common characteristics*, then I'd probably use the AppleScript approach (as @cdthomer does routinely). Otherwise I use the Set Variable to Text (%Delete%) method.

*Note that the AppleScript is not restricted to starts with. One could delete all of the variables that contain a certain string.

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

As @tiffle stated, there's no requirement to delete global variables, but my personal preference is to do so unless I want to:

  1. retain the value for a subsequent run*, or
  2. pass a value to another macro (I do this rarely).

If you don't delete them, then the Keyboard Maestro Preferences>Variables pane (or Settings>Variables with macOS Ventura) will get cluttered and it will be more difficult to find the global variables that are likely of more interest, i.e., the ones that fall into the two categories mentioned above.

*It's a bit off-topic, but I generally prefer to retain values in Dictionaries, not global variables. My rationale is here.

Also as stated above by others, you generally don't need to delete local or instance variables, but there are times when it becomes useful. For example, suppose you have a loop and you are using a Search using Regular Expression capture group to extract a color from a string. If the color is found, you set local_DetectedColor to the value. In this situation you'd want to place the following before the Search using Regular Expression:

Keyboard Maestro Export

Yes, you wouldn't necessarily need to use the %Delete% token, but I like to use it because it helps reinforces the purpose of the action.

With local and instance variable, my personal preference is to use this convention:


Legal variations of local above are:

local SomeDescriptionInCamelCase
LOCAL SomeDescriptionInCamelCase
local Some Description

There are other legal permutations, but regardless, I prefer the format I mentioned above. Here's my explanation and a few caveats:

  1. I only use two underscores for situations where they are needed, i.e., 1) with Prompt For User Input when variable names double as dialog labels; and 2) as parameters within Subroutines. That way, when I see a variable that includes two underscores e.g., local__Color I know that the variable is used in one of the special contexts.

  2. I only include spaces in variable names if the variable doubles as a label in a Prompt for User Input dialog as mentioned above. For subroutine parameters, I use the format local__SomeSubroutineParameter. Notwithstanding the dialog labels, I avoid spaces because there's the huge advantage of being able to select a variable name using ⌥⇧← or double-clicking if the variable name is devoid of spaces.

  3. It's trivial, but I prefer lowercase local and instance because with my eye, I'm able to more quickly identify the unique portion of a variable name. There's the bonus that lowercase letters are slighly narrower in the KM editor.


Well, I've just tested this and it indeed AppleScript does not delete the variable from the KM Preferences pane. It works as expected using the KM Set Variable to Text action though.

@peternlewis - is there an explanation for this?

1 Like

Sure. Variables, and where they are listed, is extremely complex. And so is AppleScript.

In Keyboard Maestro, if you set a variable to the empty string, then the variable is removed and no longer exists.

If you set it to “%Delete%” then the variable exists, but its display in variable lists is suppressed, and its value is functionally empty.

The listing of all variables is all the variables with values, plus all the variables that are referenced directly in any action (not as tokens), minus all the variables whose values are "%Delete%".

More or less.

Largely, people should simply stop concerning themselves about deleting variables, unless a variable is large (eg holding contents of a file). And they should use Local or Instance variables as appropriate.

Setting variables to %Delete% is simply a hack to have them not displayed in the variable lists (Preferences, popup menus, etc). It's not generally a good idea.


Hi @peternlewis. Thanks for weighing in.

In my test macro, it appears that empty string and %Delete% have the same effect:

Test Variables.kmmacros (18 KB)


What am I missing?

You aren't looking in the Preferences pane or in Keyboard Maestro’s Insert Variable menus…

That is the primary purpose of the %Delete%. It removes them from that.

Setting a Local or Instance variable to %Delete% is meaningless - it wont have that value outside the macro which is the only place having a %Delete% value means anything.


You're right, for local or instance variables I wouldn't expect to find them there.

Well, clearly I have been misusing the %Delete% token, but (apparently by luck), it has been a functional substitute for empty string*. Interestingly, the If Then Else (exists and is empty) action also evaluates the two as equivalent.

*I started using %Delete% with the Set Variable to Text after chasing down a nasty bug in one of my macros. I had intended to set a variable to empty string, but I had mistakenly entered an invisible linefeed. Once I discovered the bug, I went looking for a NULL token; but unless I'm mistaken, no such token is available.

Thanks again, @peternlewis, for taking the time to clarify these points.

1 Like

Thanks for the explanation, @peternlewis. IIRC the use of %Delete% came up years ago when it was discussed in the context of shortening the list of variables displayed in the KM preferences pane and also reducing the storage size of variables. To be honest, until yesterday when I performed my test I have never seen the need for using it since my list of global variables is tiny and restricted to some old legacy macros that I can’t be bothered to convert to using locals.

My message to the OP @igh_033 is still always use local/instance variables unless absolutely necessary. :grinning:


Mark this well...

Variables listed in the Variable Preferences Panel do NOT necessarily exist in memory (so to speak).

They may simply be variable names used in actions somewhere in the Keyboard Maestro Editor.

This has greatly confused a considerable number of folks over the years...


%Delete% would not really help you in that case, and invisible character (eg a space) would likely still go unnoticed.

1 Like

Maybe in general, but not in my case since I always use this simple macro to populate such actions. (That was a nasty bug that I never wanted to repeat! :grinning:)

But I get your point, @peternlewis. I could eliminate the red action and accomplish the same thing, except I do use the “%Delete%” as a visual cue.

Thanks, @peternlewis.

1 Like

Thank you for the wonderful feedback. First of all, I didn't understand the difference between local variables and global variables. So I started by searching it. And I read your text carefully. But it was very difficult, so I couldn't understand everything at the current my level. But your simple explanation is really makes sense. I will bookmark the post and read it again when it grows. :pray:

Thank you a lot.


Thank you for your kindness👍

1 Like

You’re very welcome. Don’t hesitate to keep asking questions here if you have any more.



Just keep in mind that Global Variables are available to every macro and are also Persistent.

In other words – global variables are never deleted, unless the user manually deletes them or automates the process in a macro.

Persistent variables can cause side-effects (problems) by inadvertently introducing existing values into a macro that doesn't expect there to be an existing value.

That can cause errors that will make you pull your hair out trying to find – until you understand the cause.

Local Variables only exist while the macro they're in is running and are automatically deleted when the macro exits.

Local variables are only available to the macro where they originate – therefore they don't “pollute” the global variable space and cause problems for other macros.

Hopefully this clarifies things a little bit.

1 Like