Hi @satellite-observer-4, and welcome to the Postman community!
This one had me puzzled for a while. The problem isn’t that pm.variables
stops being in-scope; the issue is due to the tests running asynchronously, and consequently your final line (which unsets tmpSeriesKey
) is being executed before the sendRequest
.
You can demonstrate this by either temporarily commenting-out the unset, or by wrapping it in a hardcoded 10-second timeout (obviously not something that you would want to do, but it will demonstrate the issue):
setTimeout(() => { pm.variables.unset("tmpSeriesKey") }, 10000);
Now, both of your console.log
statements will contain the variable.
I would recommend finding some similar way to ensure that your tests are completed prior to unsetting this key (depending on your request execution flow, this could potentially include clearing the key at the start of the next test, rather than the end of this one?) - you may also be interested in reading some more tips about how to force the order of execution: