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

Continuing the discussion from Sharing tips and tricks with others in the Postman community:

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 :slight_smile:

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.

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

code snippet

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 :sunglasses:. 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:

2 Likes

Fixed this - All the links should be there now :slight_smile:

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

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 :slight_smile:

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()
1 Like

too cool, thanks for fixing the links @danny-dainton :+1:

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 :nerd_face: :upside_down_face:

2 Likes

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

2 Likes