💡 "Postman Can’t Automatically Clear/Reset Variables"? Here’s How It Can!

Background: Community Feedback
This post was inspired by a comment from @abdulkabirsultan in a previous weekly challenge, who pointed out that environment variables can linger after runs and cause test issues.

See the community feedback and problem context

:movie_camera: Watch the Fix in Action
Here’s a 5 minute walkthrough of the simple script I use to automatically reset and clean up environment variables, no more manual clearing after every run. This approach has made my workflow far more efficient and has helped me collaborate more effectively with my teammates by keeping shared environments clean and consistent.

Package Library Clean Up Script:

const cleanupVars = (options = {}) => {
    const {
        scope = "environment",   // "environment", "collectionVariables", or "globals"
        exclude = [],            // Array of variable names to KEEP
        log = true               // Whether to log actions
    } = options;

    // Validate scope
    const validScopes = ["environment", "collectionVariables", "globals"];
    if (!validScopes.includes(scope)) {
        console.error(`Invalid scope: ${scope}. Must be one of ${validScopes.join(", ")}.`);
        return;
    }

    // Select the variable scope
    const variableScope = pm[scope];
    const allVars = Object.keys(variableScope.toObject());

    // Loop through and remove all not in exclude list
    for (const varName of allVars) {
        if (!exclude.includes(varName)) {
            variableScope.unset(varName);
            if (log) console.log(`đź§ą Cleared ${scope} variable: ${varName}`);
        }
    }

    if (log) {
        console.log(
            `âś… Cleanup complete for ${scope} variables. Preserved: [${exclude.join(", ") || "none"}]`
        );
    }
}

module.exports = { cleanupVars }

Did this fix help you out? Tell us in the comments!

And if there’s another Postman topic you’d love a quick walk through on, let us know :heart:

4 Likes

This is really handy! Thanx.

2 Likes

Hi @danny-dainton
Thanks a lot for sharing this, that’s a really elegant approach!

I actually had a basic cleanup workaround for this earlier, mainly to clear transient variables after each run:

pm.test("Clean transient variables", function () {
    const keepVars = ["baseUrl", "authEmail", "authPassword"]; // vars I want to keep
    const allEnvVars = pm.environment.toObject();
    
    Object.keys(allEnvVars).forEach(varName => {
        // Delete vars that match common runtime patterns
        if (!keepVars.includes(varName) && /(id|token|order|session)/i.test(varName)) {
            pm.environment.unset(varName);
            console.log(`Removed ${varName}`);
        }
    });
});

It’s not as flexible as your packaged cleanup function, but it was handy for quickly removing transient variables like IDs or tokens after each run.

I really like your approach better though especially the scope parameter and the ability to exclude variables dynamically.

2 Likes

Thank you for the suggestion during the challenge. :trophy:

I did something about 6 years ago which looked very similar but this new script has a little bit more flexibility and configuration. It’s also not limited to only environments.

1 Like

Wow, this is really impressive! I’m sure many people must have come up with creative workarounds for it. Hopefully, Postman adds this as a built-in feature soon.