Whatever message this page gives is out now! Go check it out!

setRetainAll

Last update:
May 18, 2026
Retains only elements in the target set that are also in the source set.

Description

Keeps only elements in the target set that are also present in the source set.

Returns

Void

Category

ColdFusion Set built-in functions

Function syntax

setRetainAll(targetSet, sourceSet)

Parameters

ParameterDescription
targetSetSet to mutate.
sourceSetElements to keep.

See also

Example

The following example uses this API in cfscript (syntax: setRetainAll(targetSet, sourceSet)).

 <cfscript>
    a = setNew();
    a.add(1);
    a.add(2);
    a.add(3);

    b = setNew();
    b.add(2);
    b.add(3);
    b.add(4);

    setRetainAll(a, b);
    writeOutput(a.size()); // 2  (only 2 and 3)
</cfscript>
      

Real-world example

Filter a cart to only SKUs that are still on the allowlist after a catalog refresh.

<cfscript>
    allowed = setNew();
    allowed.add("SKU-A");
    allowed.add("SKU-B");
    allowed.add("SKU-C");

    cart = setNew();
    cart.add("SKU-A");
    cart.add("SKU-Z"); // not allowed

    setRetainAll(cart, allowed);
    writeOutput(cart.has("SKU-Z")); // false
    writeOutput(cart.has("SKU-A")); // true
</cfscript>
      

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page