Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
When placed in the Postman 'Tests' tab, this script will unset all the variables that start with a specifically given prefix so that it's slightly different from the .clear() built-in fuction.
function cleanup() {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
if (arrItem.startsWith("some_prefix")) {
pm.environment.unset(arrItem)
}
})
}
cleanup()
@nanacleto

This comment has been minimized.

Copy link

nanacleto commented Oct 15, 2019

Hello,
But how you set this to run at end of the collection run?
you have a "last request" where after you run the clean variables?

It exists some way to postman knows the end of collection run?

@DannyDainton

This comment has been minimized.

Copy link
Owner Author

DannyDainton commented Oct 15, 2019

I knew the what the final request was in my Collection so I have in run within there because it worked in my context.

Depends on the way that you structure your own Collections, you could have a folder that contains a request that tearsdown the data following a Collection run.

One of the Postman community members write about how he does this in his own context.

https://community.getpostman.com/t/my-code-snippets-from-the-london-postman-summit-paul-farrell/4591

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.