How do you clear a global variable?

I’m stuck updating some old v2.9.x scripts to work with v3.17 but I can’t find a way to clear a defined global variable.

Can anyone post the command(s) necessary to clear an already defined global variable?

I’ve reviewed the only available documentation (http://wiki.mikrotik.com/wiki/Scripting) but it doesn’t even mention clearing global variables.

Cheers

By clearing you man delete it? It is not possible, only thing you can do is to set it to nil.

Greetings!
The variables are in
/system script environment
Do a print and then use
remove X
where X is the line number of the variable you want to delete.

Thanks for the replies the ‘/system script enviroment’ method works great. I was really getting tired of unneeded global variables.

Cheers

Still impossible ?

I use the NO-IP mikrotik scrip, and every now and then, I run this command “/system script environment remove [find user=“admin”]” to remove everything, so that I can do a forced IP update

but of course now after a upgrade , the old scripts doesn’t work!!! :frowning:

I use the NO-IP mikrotik scrip, and every now and then, I run this command “/system script environment remove [find user=“admin”]” to remove everything, so that I can do a forced IP update

but of course now after a upgrade > , the old scripts doesn’t work!!!

There is no “user” field in “/system script environment”. That is probably why it is not running.

exactly… older versions of ros always said user=admin … new version…nope

I’ve changed all the :global to :local ..no more issue’s

Starting with v6.2 you can unset global variables:

[admin@pe0] /system> :global aa 1
[admin@pe0] /system> :environment print 
aa=1

[admin@pe0] /system> :set aa 
[admin@pe0] /system> :environment print 

[admin@pe0] /system>

Script to clear all variables

:if ([:len [/system script environment find]] != 0) do={
:for E from=0 to=([:len [/system script environment find]] - 1) do={
/system script environment remove $E
}}

Could just use:

/system script environment remove [find]

The command doesn’t complain if nothing is found.
And you can easily make it only delete a variable with a specific name if need be.

/system script environment remove [find name="ThatVar"]

When using WinBox you can also go to “System → Scripts → Environment tab” where you have a full list of all global variables and can delete any you you want. You can even change the value of a global variable in this menu.

Remember that you need FULL ADMIN permissions to do either of these.

Thanks, I was not aware that you can simply add a [find] at the end of the command to remove.

The “[find]” simply selects all objects that matches the search criteria and added them to an array.
If there is more than one object the “remove” command is simply repeated for each object in the array. Same way as if you had done a “:foreach” loop.