Can I clear just the "current values" in an environment

Hi All,

I have the below code in a pre-request script at the start of my test run. This acts as a “Set Up” so all environment variables are cleared/initialized to remove any persisting data from other test runs.

function clearVariables() {
    // Get all the names of our env variables and put them in an array
    const environmentVariables = pm.environment.values.map(function(variable) {
        return variable.key;
    });
    // Filter through the above array but don't add variables that:
        // - Contain the string 'url' in the variable name
        // OR
        // - Are Named 'auth-token'
    const binTheseVariablesOff = environmentVariables.filter(function(variable) {
        return !variable.includes("url") && !variable.match("auth-token");
    });
    // Now go through this new array and delete these env variables
    return binTheseVariablesOff.forEach(function(variableName) {
       pm.environment.unset(variableName);
    });
}
// Call the function
clearVariables();

Now this kinda works, but the problem with it is that it’s effecting other users that using the same environment.

Example:

  • There is a collection called “Regression Tests”
  • There is an environment called “Env - Regression Tests”
  • Person A and Person B are running some tests on the above collection and environment
  • Person A creates an environment variable called “name: John”
  • As soon as Person B hits the request that calls the above function to clear env variables, the environment variable(s) that Person A have/have created are now gone.

A more real-life example would be - my tests are running in CI and if I call the above function in the Postman application, this will remove all env variables from that environment and cause the tests to fail in CI.

Is there a way to just clear the current values? I don’t want other users to be affected by this as the above function clears the actual env variables (current and initial value)

Is this intended?
Can I do this another way?

I’m using Postman version 6.7.4 if that helps

@pfarrell I see you have whitelisted the variables that do not need to be deleted.

How about blacklisting all the variables that need to be removed, so whenever the script runs all the variables that need to be unset, only those are removed?

Then it won’t affect the other people who add their own variables.

Another possible workaround:

Instead of unsetting the variable, you can set its value to null.
Updating the unset line in the script to the following:

pm.environment.set(variableName, null);

So only current value is updated to be null and the variable is not removed totally.
Also along with this,
The users who’re locally working on their desktop apps can turn off automatically persist variables, so that the variable values do not get synced until intended.

Docs on Automatically persist variable values section: https://learning.getpostman.com/docs/postman/launching_postman/settings/

2 Likes

@sivcan just to clarify, setting the environment variable to null appears to actually retain the current value as null. A get still doesn’t give you back the initial value. Is that what you were expecting?

I don’t see any way to wipe the current value and return it to the initial value.

@hod.greeley,

I don’t know of a way to programmatically Reset All from a request script without making an external call to the Postman API using pm.sendRequest.

You can get the current Environment ID with something like this in the script: pm.environment.id.split('/')[1]. To translate this, the API URL to hit would be

`https://api.getpostman.com/environments/${pm.environment.id.split('/')[1]}`

You’d need to set an X-Api-Key with a Postman API key (likely taken from another variable).

You’d get a response back of all the persisted values, like so:

{
    "environment": {
        "id": "24ed28bc-10c7-4a2d-806a-0cad1abc792a",
        "name": "cosmos",
        "values": [
            {
                "key": "baseUrl",
                "value": "https://e8c086f7-a5c9-4752-b81f-bfac0d0dc5d2.mock.pstmn.io",
                "enabled": true
            }
        ]
    }
}

Then with pm.response.json(), you can iterate through this structure and pm.environment.set the saved keys to the saved values.

A bit cumbersome, but it would work.

Hope this helps.

Best,

Kevin

Thanks for the great write-up @kevin.swiber. For my use, I’m want to put in checks to make sure someone has set certain values before executing a call. That way I can trap common errors like not having set a user name parameter.

This looks like it could do the trick, Since the number of checks I need to perform is small, I chose to keep copies in the collection variables. I compare those to the environment version simply to make sure the latter has been altered.

Oh, to be more clear, I was looking at resetting current to initial as another workaround. I thought to save current, reset it, compare to initial. Obviously an easier approach would be to have some way of determining directly whether current had changed. I don’t see this as a major use case, though.

2 Likes