Deleting all Variables - aren't they re-created on running a macro?

I did look around the forum before posting, don’t see my answer.
In the last 2 weeks or so of working with KM, I have downloaded/imported a number of macros to learn from, and it’s been very helpful.
But now I have this tremendous list of variables that I don’t really want to keep around since I’m just starting with KM, I’d like to keep it clean.
? Can I delete all variables and when a macro is run that ‘sets a variable’ isn’t that variable created at that point? So I can delete all variables and not worry? Or is that not how it works?

  • I do understand that the best thing to do is to clean up the variables that are created in a macro by deleting them at the end of the macro. I guess a number of the examples I checked out did not do so.
    Any thoughts appreciated.

Yes, you can generally delete all your variables.

Variables can include state that you might not want to delete, so it does depend a bit on the macros you are using.

But essentially deleting a variable just sets it back to the empty string and it will be recreated by the macro as needed.

Variables will show up in lists even after being deleted if they are used by any macro.

You can explicitly set a variable to %Delete% and ti will then not show up in lists.

Great, thank you ~

Peter is correct that, in general, you can delete all variables and they will be recreated. However, this could cause loss of data for some macros, that accumulate data in variables over time, or that save the user's last usage of certain variables. So, before you just delete all variables without review, you might want to review any macros you use that might accumulate data.

It is considered good macro design by many of us to delete all variables used in a Macro, that will not be needed in the future, at the end of the macro.
You can do this manually, one variable at time, using this type of KM Action at the very end of your macro:

It is also a good idea to use some naming convention to identify the variables that should NOT be deleted. I use this convention, to PREFIX variables that should not be deleted:
DND__

The "DND" stands for "Do Not Delete".

However, there are many Macros published in the Forum that do not cleanup its variables. To help us cleanup (delete) variables we no longer need, there have been several Macros and scripts published to do this.

See this Forum Search:
#macro delete variables

In particular, these Macros might be helpful:

I also have a script that will delete all variables that start with a prefix or are in a list in another KM Variable:

tell application "Keyboard Maestro Engine"
  
  --- DELETE ALL KM VARIABLES WITH THIS PREFIX ---
  
  set prefixStr to getvariable "KMVarPrefix"
  
  if (prefixStr ≠ "") then
    set value of variables whose name starts with prefixStr to "%Delete%"
    display notification "All KM Variables with PREFIX = '" & prefixStr & ¬
      "' have been deleted." with title "Keyboard Maestro Script"
  end if
  
  --- DELETE KM VARS IN THE VARIABLE "KMVars_To_Delete" ---
  
  set varsToDeleteList to paragraphs of (getvariable "KMVars_To_Delete")
  
  if (varsToDeleteList ≠ {}) then
    
    set varsToDeleteList to varsToDeleteList & {"KMVars_To_Delete"}
    
    repeat with oVar in varsToDeleteList
      setvariable (oVar as text) to "%Delete%"
    end repeat
    
    display notification "All KM Variables in 'KMVars_To_Delete' have been deleted." with title "Keyboard Maestro Script"
    
  end if
  
end tell

Before you run this script, just set these two KM Variables:

And then a "Execute AppleScript" Action, using the script from above.

I will try to post this as a macro tomorrow to make it easy for everyone to use.

Please let me know if you have any questions.

3 Likes