How can I automatically remove a discount code from the cart based on a condition?

I have a script in my cart.liquid code that auto-replaces the discount code in the cart based on customer tag like this –

//automatically apply NONMEMBERDISCOUNT to the cart

//if customer tag contains “member” then automatically replace the discount code with MEMBERSONLYDISCOUNT

{% assign customersTagsDowncased = customer.tags | downcase %}
{% assign allTags = customersTagsDowncased | join:‘,’ %}
{% assign found_member = false %}
{% if allTags contains “member” %}

{% assign found_member = true %}

What I would also like to do is automatically remove the discount code entirely based on a condition, e.g. when a non-member tries to use the member’s only discount code, something like this –

//if customer tag contains “group2” then automatically remove the discount code if found

{% assign customersTagsDowncased = customer.tags | downcase %}
{% assign allTags = customersTagsDowncased | join:‘,’ %}
{% assign found_member = false %}
{% if allTags contains “group2” and %}

Is this possible?