# Yet another way to clean your environment variables (but still keep some)

**URL:** <https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456>\
**Category:** Help Hub\
**Created:** [May 15, 2019, 9:39pm UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456 "2019-05-15T21:39:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [May 15, 2019, 9:39pm UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456/1 "2019-05-15T21:39:19Z")

</div>

Continuing the discussion from [Sharing tips and tricks with others in the Postman community](https://community.postman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123):

In @danny-dainton’s topic above, he shared a way to keep variables with a given prefix, it was funny when I was reading it last night because I had to solve the exact same problem the day before. My solution is just a little different from Danny’s and thought I would share it too 🙂

In essence, if and when I want to clean up my environment variables, I set env var `OkToClear` to `'true'`, and then use an array `varsToKeep` that lists the variables I want to keep.

```auto
OkToClear = 'true'
varsToKeep = ["OkToClear","HostURL","debug","assetsIfModifiedSince","varsToKeep"]
... other vars ...

```

code snippet

```javascript
if( pm.environment.has("OkToClear") 
  && pm.environment.get("OkToClear")==="true" 
  && pm.environment.has("varsToKeep")
) {
  const varsToKeepJSON = pm.environment.get("varsToKeep");
  let varsToKeep, tmp = {};
  // ignore JSON parsing errors
  try{varsToKeep = varsToKeepJSON && JSON.parse(varsToKeepJSON);}catch(e){} 
  
  // save the variables to keep to tmp
  varsToKeep && varsToKeep.forEach(
    variable => tmp[variable] = pm.environment.has(variable) && pm.environment.get(variable)
  );

  // clear the whole environment
  pm.environment.clear();

  // save all the variables we kept back to the environment
  varsToKeep && varsToKeep.forEach(
     variable => tmp[variable] && pm.environment.set(variable, tmp[variable])
  );

  // log the clean environment to console
  console.log("Environment cleared to");
  console.log(pm.environment.toObject());
}else{
  console.log("Did not clear Environment variables. Check that Environment variable OkToClear = true");
}

```

Notes:

1. I’m using **short circuiting** a bit here, which where commands prefixed by a logical expression are only executed when the logical expression evaluates to **truthy**. For example, `varsToKeep` is only assigned a value if `varsToKeepJSON` has a value.
2. The `try{...}catch(e){}` is used to ignore exceptions during the JSON parsing. This is safe to do here because we always check that `varsToKeep` is truthy before using it.
3. The **arrow functions** are a bit cryptic, but once you understand them they are way cool 🕶. I will try and write a separate article on them, but in essence, for this example, they are simply 1 line functions used on each element of an array.
4. The reason for the `OkToClear` flag is to make sure that when I share the collection with my colleagues that my code snippet doesn’t inadvertently delete all their environment variables which they may have carefully setup for their test

links:

- **short circuiting**  
[Expressions and operators - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation)
- **truthy**  
[Truthy - Glossary | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)
- **arrow functions**  
[Arrow function expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 16, 2019, 8:25am UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456/3 "2019-05-16T08:25:08Z")

</div>

Fixed this - All the links should be there now 🙂

Thank you so much for sharing this awesome tip with the Postman community! 🏆

I was also doing it a different way before making it a bit more dynamic, it was becoming a bit of an admin burden to add more names to an array, as my collection grew bigger 🙂

```
function cleanup() {
    const clean = ['first_name', 'last_name', 'total_price', 'depositPaid', 'check_in', 'check_out', 'additional_needs']
    for(let i = 0; i < clean.length; ++i){
        pm.environment.unset(clean[i])
    }
}
cleanup()

```

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [May 16, 2019, 8:43am UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456/4 "2019-05-16T08:43:20Z")

</div>

too cool, thanks for fixing the links @danny-dainton 👍

Totally agree, your way is much less code and certainly best go-to if you have full control of your variables. For me I was inheriting a collection of 150+ requests being maintained by our dev’s and they were not going to like me putting prefixes on their variables so I came up with my own homebaked solution with the list of vars to save 🤓 🙃

---

<div class="post-metadata">

**Author:** ![tmccann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/tmccann/32/3550_2.png) [@tmccann](https://community.postman.com/u/tmccann)\
**Post date:** [May 16, 2019, 6:30pm UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456/5 "2019-05-16T18:30:59Z")

</div>

Love this approach as this is something I have pondered on how to keep my envs clean with less risk of cleaning out things I want to keep.

Great work @VincentD123

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 12, 2024, 2:47pm UTC](https://community.postman.com/t/yet-another-way-to-clean-your-environment-variables-but-still-keep-some/5456/6 "2024-12-12T14:47:56Z")

</div>

Closed on December 12, 2024, 2:47pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
