# Set multiple collection variables from an array

**URL:** <https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646>\
**Category:** Help Hub\
**Tags:** variables, collections\
**Created:** [October 14, 2024, 3:02am UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646 "2024-10-14T03:02:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 3:02am UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/1 "2024-10-14T03:02:08Z")

</div>

Bit of newbie here , so need some help from Postman experts out there 🙂 ,

**Trying to achieve** : as part of my tear down, I’m trying to set “NULL” / BLANK for all the existing collection variables.

**Problem** : tried 2 logics but both of them DO NOT set any of the collection variables to NULL / BLANK.

**Logic#1** :

```auto
// getting all collection variables
const allCollVariables = pm.collectionVariables.values.map(function(variable) {return variable.key; });

// filtering out base_url variable
const excludeBaseURL = allCollVariables.filter(function(variable) {        
             return !variable.toLowerCase().includes("base_url"); });
 
// clearing all the remaining variables
        return excludeBaseURL.forEach(function(variableName) {
           pm.collectionVariables.set(variableName, null); });

```

**Logic#2** :

```auto
// getting all collection variables
const allCollVariables = pm.collectionVariables.values.map(function(variable) {return variable.key; });

// filtering out base_url variable
const excludeBaseURL = allCollVariables.filter(function(variable) {        
             return !variable.toLowerCase().includes("base_url"); });
 
// clearing all the remaining variables
for (var i = 0; i < excludeBaseURL.length; i++) {
                pm.collectionVariables.set(excludeBaseURL[i], null);
        }

```

**Note** : I tried using " " instead of null, but it didn’t work…

Can someone please help me out here !

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 14, 2024, 11:36am UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/2 "2024-10-14T11:36:19Z")

</div>

Hey @koustubh24 👋

Welcome to the Postman Community! :postman:

Could you something like this to set them to a blank value?

```auto
const resetCollectionVars = (prefix) => {
    const variableKeys = Object.keys(pm.collectionVariables.toObject());
    variableKeys.forEach((arrItem) => {
        if (!arrItem.startsWith(`${prefix}`)) {
            pm.collectionVariables.set(arrItem, "");
        }
    });
}

resetCollectionVars("base_url");

```

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 8:00pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/3 "2024-10-14T20:00:02Z")

</div>

thank you @danny-dainton for that… I copied that in the Collection \> Post-response script and tried referencing in one of the request’s Post-response script but it didn’t work … is there a particular way to reference it?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 14, 2024, 8:02pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/4 "2024-10-14T20:02:30Z")

</div>

There are a few ways but I would recommend using the Package Library to add the script.

> **[Reuse scripts in Postman | Postman Learning Center](https://learning.postman.com/docs/tests-and-scripts/write-scripts/package-library/)**
>
> Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 8:04pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/5 "2024-10-14T20:04:24Z")

</div>

Thank you @danny-dainton

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 8:11pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/6 "2024-10-14T20:11:15Z")

</div>

Unfortunately, can’t see the “Package” option in my Postman (v11.16.1).

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 14, 2024, 8:18pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/7 "2024-10-14T20:18:43Z")

</div>

Is that not here?

 ![17289370633023457250723877415404](https://us1.discourse-cdn.com/getpostman/original/3X/d/6/d6b3365bcf3cf13d8bd783ddc9305c4f30b4900b.jpeg)

Do you have this in a Public Workspace?

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 8:22pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/8 "2024-10-14T20:22:39Z")

</div>

I’m using a “shared-workspace” with just 1 other tester & this is what I see. …

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/e/cede93cc7d70196f2e0f30b596f4f5afccde7b82.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 14, 2024, 8:29pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/9 "2024-10-14T20:29:35Z")

</div>

I can’t see the Workspace visibility from that image.

The package library was added for v11 so it would be part of your version.

The only way it wouldn’t be showing is if you’re working in a Public Workspace.

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 14, 2024, 9:04pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/10 "2024-10-14T21:04:04Z")

</div>

sorry about that, but here is a “private” workspace of mine & I still can’t see the package options …  
Collection:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/d/fdc3d0a79eede686917d124ade0b097d19dcf037.png)

Workspace settings:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/2/72e58d01e827b6602c124e00c3bbca6f7571943b.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 15, 2024, 11:20am UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/11 "2024-10-15T11:20:47Z")

</div>

That’s strange, if you’re using V11 and not in a Public Workspace, you should see that.

Could you share more details about your OS and other environment setup, please?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 15, 2024, 1:21pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/12 "2024-10-15T13:21:16Z")

</div>

In the meantime, you could use the same method here to reuse the scripts in your Collection.

[https://www.postman.com/postman/postman-answers/collection/xz5vqwi/using-global-utility-functions?action=share&source=copy-link&creator=1794236](https://www.postman.com/postman/postman-answers/collection/xz5vqwi/using-global-utility-functions?action=share&source=copy-link&creator=1794236)

Store as a Global Variable:

```auto
utils = {
    resetCollectionVars: (pm, prefix) => {
        const variableKeys = Object.keys(pm.collectionVariables.toObject());
        variableKeys.forEach((arrItem) => {
            if (!arrItem.startsWith(`${prefix}`)) {
                pm.collectionVariables.set(arrItem, "");
            }
        });
    }
};

```

In your script:

```auto
eval(pm.globals.get("utils"));

utils.resetCollectionVars(pm, 'base_url')

```

This is a workaround but I would recommend using the Package Library once we sort out what’s happening there.

---

<div class="post-metadata">

**Author:** ![koustubh24](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@koustubh24](https://community.postman.com/u/koustubh24)\
**Post date:** [October 15, 2024, 8:31pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/13 "2024-10-15T20:31:21Z")

</div>

Thank you @danny-dainton for that solution. As for the details, they’re as follows:

OS = Windows 10 Enterprise & I’m behind a secure firewall (Zscaler).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [November 14, 2024, 8:32pm UTC](https://community.postman.com/t/set-multiple-collection-variables-from-an-array/69646/14 "2024-11-14T20:32:08Z")

</div>

Closed on November 14, 2024, 8:32pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
