# When do i clear an environment variable

**URL:** <https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256>\
**Category:** Help Hub\
**Created:** [August 29, 2021, 9:19am UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256 "2021-08-29T09:19:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [August 29, 2021, 9:19am UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/1 "2021-08-29T09:19:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [August 29, 2021, 11:12pm UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/2 "2021-08-29T23:12:59Z")

</div>

before collection run ? or before a particular request

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [August 30, 2021, 6:49am UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/3 "2021-08-30T06:49:37Z")

</div>

its for a collection run

---

<div class="post-metadata">

**Author:** ![kitty-garrison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kitty-garrison/32/27117_2.png) [@kitty-garrison](https://community.postman.com/u/kitty-garrison)\
**Post date:** [August 30, 2021, 9:27pm UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/4 "2021-08-30T21:27:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [August 31, 2021, 10:15am UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/5 "2021-08-31T10:15:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kitty-garrison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kitty-garrison/32/27117_2.png) [@kitty-garrison](https://community.postman.com/u/kitty-garrison)\
**Post date:** [August 31, 2021, 9:12pm UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/6 "2021-08-31T21:12:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [August 31, 2021, 9:49pm UTC](https://community.postman.com/t/when-do-i-clear-an-environment-variable/27256/7 "2021-08-31T21:49:57Z")

</div>

thanks, i really like the last solution!
