Skip to content

Instantly share code, notes, and snippets.

@DannyDainton
Last active October 15, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DannyDainton/0a204d53e72464029a5aca8b2ae1592f to your computer and use it in GitHub Desktop.
Save DannyDainton/0a204d53e72464029a5aca8b2ae1592f to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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