How to remove (indirect) parent element with class="foobar" only?

How to remove (indirect) parent element with class="foobar" only?

Assume we have the following html structure:

  1. <div class="aaa"><div ....>...
    <div class="bbb">...</div>
    </div></div>
    <div class="foobar"><div ....>...
    <div class="bbb">...</div>
    </div></div>

Now I want to do the following:

- Search for all <div> elements with class="bbb"
- then (for each found element) go (one or more levels) upwards and search for a(nother) <div> element with class="foobar"
- If it exist then remove this <div> element with class=foobar. Otherwise do nothing

It the sample above only the second part should be removed.

How can I achieve this?

Peter