Learn from experts and connect with global tech leaders at POST/CON 24. Register by March 26 to save 30%.

Learn more →
X

Pro-tip: Dynamically Unset Postman Environment Variables

Avatar

Manually clearing out environment variables that are set during a collection run can be long and tedious. In this quick pro-tip post, I’ll walk you through how to save time by dynamically unsetting variables.

In this example, we’ll use the `pm.environment.toObject()` method to get all variables in the environment.

You’ll need the following:

  • The Lodash module, which is an awesome utility module that simplifies JavaScript by taking the pain out of working with arrays, numbers, objects, string, etc.
  • The _.keys() function to get a list of all the keys within the `pm.environment.toObject()` object
  • The _.each() function to iterate through these elements
function cleanup() {
    const clean = _.keys(pm.environment.toObject())
    _.each(clean, (arrItem) => {
        if (arrItem.startsWith("some_prefix")) {
            pm.environment.unset(arrItem)
        }
    })
}

To unset the variables with the “demo” prefix, you can use an ‘if’ statement and use the `startsWith()` method to grab the ones desired.

For this example, we’ve manually added a few variables into an environment file. If this were put into a workflow, these variables would have been created during a collection run, using the `pm.environment.set()` function (see Postman Sandbox API reference).

Below, you can see that some of the variables are labeled with a “demo” prefix.

We will be clearing out the variables with the “demo” prefix. To edit environment variables click the Manage Environments icon (the gear icon) on the top right in the Postman app. From there you can create a new environment, add variables, and share environments with your team.

You can also edit current environment variables by clicking the Quick Look icon (the eye icon) in the upper right and then click Edit to change variables and values.

 

In your code, you can change “demo” to any prefix matching the unwanted variables in your environment.

Hint: If you know you are adding variables that you need to delete later, name them with a unique prefix so you can easily delete them through this process later.

Now that you have everything set up, you can simply send your request. All of the variables with the prefix you chose should be deleted from your environment, just as in the screen below.

And ta-da! You have quickly and dynamically unset Postman environment variables!

This blog post was adapted from Danny Dainton’s original post on Postman’s Community forum.


Initial vs Current Values for Variables in Postman

If you’re working with variables in Postman, you might be wondering “what’s the difference between initial value and current value”. Joyce Lin, Director of Developer Relations, explains in under a minute.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 thought on “Pro-tip: Dynamically Unset Postman Environment Variables

  • Avatar

    Another option is to have a Collection level variable to specify which Env variables to be cleared:
    pm.collectionVariables.get(“temp-variables”).split(“,”).forEach(varName => pm.environment.unset(varName));

    “temp-variables” is a variable predefined on the Collection level: “user-id,order-id”
    This option is easier if you have already designed your tests and cleanup is needed between tests