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