When do i clear an environment variable

I have a collection run which sets new environment variable on each run. I am constantly adding to the collection so the last test in the collection is changing. If i want to clear certain environment variable before the collection runs each time - where do i do this? Could i put it as a pre-request script before the first test or is there a better way?
I know ideally it should be after the collection has finished running each time but at this point i dont know where the end is as mentioned above…

before collection run ? or before a particular request

its for a collection run

I wonder if there may be a better place to set that variable to begin with. If the values you are setting are only used during a collection run a “Local Variable” set in the first request would be available within each request in the run, and clear once the run has finished.

Set a Local Variable with:
pm.variables.set("varName", "varValue");

Get a Local Variable with:
pm.variables.get("varName"); in the scripts or just like other variables {{varName}} in the URL or headers.

you can use pm.environment.unset to unset a variable , you can also use pm.variables as mentioned

I think the OP is looking for the where and not necessarily the how. I tried to find a solution that involved some kind of check to see if it was the first/last call in the run and be able to store the script at the folder level but I had no luck.

Another solution I considered, if they must use env vars, is to make a dummy call, something like GET https://postman-echo.com/get, keep it at the end of the collection and call it “Clear-Vars”. Then they would be able to place any pm.environment.unset("varName")s they needed in that call.

thanks, i really like the last solution!

1 Like