# Pro-tip: Dynamically Unset Postman Environment Variables

Manually clearing out [environment variables](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmenvironment) that are set during a collection run can be long and tedious. In this quick pro-tip post, I’ll walk you through how to save time by dynamically unsetting variables.

In this example, we’ll use the [`pm.environment.toObject()` method](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmenvironment) to get all variables in the environment.

You’ll need the following:

- The Lodash[ module](https://lodash.com/docs/4.17.11), which is an awesome utility module that simplifies JavaScript by taking the pain out of working with arrays, numbers, objects, string, etc.
- The [\_.keys()](https://lodash.com/docs/4.17.10#keys) function to get a list of all the keys within the `pm.environment.toObject()` object
- The [\_.each()](https://lodash.com/docs/4.17.11#forEach) function to iterate through these elements
 
 ```
function cleanup() {
    const clean = _.keys(pm.environment.toObject())
    _.each(clean, (arrItem) => {
        if (arrItem.startsWith("some_prefix")) {
            pm.environment.unset(arrItem)
        }
    })
}
```

To unset the variables with the “demo” prefix, you can use an ‘if’ statement and use the `startsWith()` method to grab the ones desired.

For this example, we’ve manually added a few variables into an environment file. If this were put into a workflow, these variables would have been created during a collection run, using the `pm.environment.set()` function ([see Postman Sandbox API reference](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/)).

Below, you can see that some of the variables are labeled with a “demo” prefix.

 ![](https://lh5.googleusercontent.com/450FFRKdC_rymOWnXu2SqnAVQxNt-Tm2yEg-YQJu3Qx2Z21UwNKrvk9yPEH-9-g_YiehwLjkFUflU_td8ncnQzoDWBf8pPzVjm3cyi8phf8m2amqUXYX01m-eq9ihqc5g5I0tvQf)We will be clearing out the variables with the “demo” prefix. To edit environment variables click the *Manage Environment*s icon (the gear icon) on the top right in the Postman app. From there you can create a new environment, add variables, and share environments with your team.

You can also edit current environment variables by clicking the *Quick Look* icon (the eye icon) in the upper right and then click *Edit* to change variables and values.

 ![](https://lh6.googleusercontent.com/itWIqHA52awzSFjbWeyWjbZ6jJddXKUVYIva8lK1OGEcbT6rP7WeisvHMUPmppYi1nRejQFYO0JpIhSxJ4E9K4AIBU-usfQgqZGogvmsUHJTn6j5WxgcZu9gmlEvesClrSrZbIb9)In your code, you can change “demo” to any prefix matching the unwanted variables in your environment.

Hint: If you know you are adding variables that you need to delete later, name them with a unique prefix so you can easily delete them through this process later.

Now that you have everything set up, you can simply send your request. All of the variables with the prefix you chose should be deleted from your environment, just as in the screen below.

 ![](https://lh6.googleusercontent.com/1EyFWgf6zxkZsjUmv2rn-VVWYR4TMAz5BALypJgDs_45PJaPBzMO9RWXBsHYr18ccNCqXktNqAcgXY8UWySrHmIuwI_Oqrw25f4CDFdEidy2lGWwUxwq5x_dfOAyEnVqKRJc5qrX)And ta-da! You have quickly and dynamically unset Postman environment variables!

*This blog post was adapted from [Danny Dainton's original post](https://community.getpostman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123) on Postman's Community forum.*

---

## Initial vs Current Values for Variables in Postman

 If you're working with variables in Postman, you might be wondering *"what's the difference between initial value and current value".* Joyce Lin, Director of Developer Relations, explains in under a minute.