Deleting variables failing sometimes

I'd like to delete the variables at the end of a macro just to clean up things. Or, other macro's depend on the fact that they exist or not.

I made a routine like this:

or I do an AppleScript like this:

tell application "Keyboard Maestro Engine"
	set varlist to name of (every variable whose name starts with "QM")
	repeat with delvar in varlist
		delete variable delvar
	end repeat
end tell

But in both cases they leave some of the variables existing, mostly when the value is empty.
When I do this script:

tell application "Keyboard Maestro Engine"
	set varlist to exists variable "QMtrigger"
end tell

I get an empty list but I do see occurrences in the Variables List in KM preferences.


Also MichaelTX's script:

leaves me with empty variables in the Variables tab of Preferences.

I think I can work around this but it is confusing to me.

Somewhere on the forum Peter has explained why some variables will show up in the Preferences window, even if they’re empty. I can’t remember his explanation, nor find it at the moment, but here’s what I do to delete my variables. I run an AppleScript to set the variables contents to BLANK first, and then delete them. The example below shows how to delete all variables that are prepended with debug__. I’ve been doing this for a year or two with no issues.

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

But it still looks like it fails here.
I first manually deleted the variables that start with QM in the preferences variables tab. Then I ran my macro a few times, and then I checked.

I get this. They are even filled with data.

Interesting. What happens if you run just that AppleScript again? Are the variables deleted at that point?

If I run it in Script Editor it deletes them all, as it should. That's odd isn't it?

if I click on TRY it also works...

Wait, I think my macro above this is aborting somehow, although it does what it needs to do, except for deleting the variables.

I think your solution is the answer, or a great workaround. Thanks!

That way my suspicion, and I actually intended for you to run that AppleScript from within Keyboard Maestro, but it sounds like you got it figured out, kudos!

Don’t hesitate to reply if you run into any other issues!