'Postman: How to delete/clear postman environment variable at run-time
Is there any way to delete/clear postman environment variable using functions at runtime. I could possibly set to blank or some special value but is there a general way of doing things.
Solution 1:[1]
Ok - so this does it.
postman.clearEnvironmentVariable("key");
Solution 2:[2]
And the right one syntax is:
pm.environment.unset("key");
Solution 3:[3]
I'v also come across this possible solution for setting the variables to blank: https://community.postman.com/t/can-i-clear-just-the-current-values-in-an-environment/6176
I have found that using the pm.environment.clear() or pm.environment.unset(variableName) deletes the variable from the environment. But sometimes I want to keep the variable and just clear the value, e.g. I want to share the environment with someone else but it contains values which should only apply to myself (such as oauth credentials).
So I re-used the function described in the link and use a pm.environment.set(variableName, "") instead of an unset.
So my function looks like:
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 as per conditions
const binTheseVariablesOff = environmentVariables.filter(function(variable) {
return !variable.toLowerCase().includes("auth");
});
// Now go through this new array and clear these env variables
return binTheseVariablesOff.forEach(function(variableName) {
pm.environment.set(variableName, "");
});
}
// Call the function
clearVariables();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pang |
| Solution 2 | Mirsad |
| Solution 3 | Xty Dee |
