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:
- MACRO: [KM] DELETE All Variables Except Those on Keep List
- MACRO: [KM] DELETE List of KM Variables [SUB-MACRO]
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.