magento2 – Delete a configarables simple product from cart in observer


I’m currently able to change the way the cart works by updating the qty value in the cart, i do this by first deleting the item before it is re-added using the checkout_cart_product_add_before observer.

Here is my observer class (the simple products work but the configurable products do not delete)

<?php
namespace XXXXObserver;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCheckoutModelSession as CheckoutSession;
use MagentoCheckoutModelCart;
use MagentoConfigurableProductModelProductTypeConfigurable;

class DeleteAndReAddFromCart implements ObserverInterface {
    protected $request;
    protected $_cart;
    /** @var CheckoutSession */
    protected $checkoutSession;
    protected $configurableproduct;
    public function __construct(MagentoFrameworkAppRequestInterface $request, cart $cart, CheckoutSession $checkoutSession, Configurable $configurableproduct) {
        $this->_cart = $cart;
        $this->request = $request;
        $this->checkoutSession = $checkoutSession;
        $this->configurableproduct = $configurableproduct;
    }
    public function execute(MagentoFrameworkEventObserver $observer) {
        $info = $observer->getInfo();
        $order = $observer->getEvent()->getOrder();
        $product = $observer->getProduct();
        $productId = $observer->getProduct()->getId();
        $prodtype = $observer->getProduct()->getTypeId(); //is configarable or simple
        $info = $observer->getInfo();
        $items = $this->_cart->getItems();
        $itemCount = count($items);
        if ($itemCount > 0) {
            if ($prodtype != 'configurable') {
                foreach ($items as $item) {
                    if ($prodtype != 'configurable') {
                        if ($item->getProduct()->getId() == $productId) {
                            $itemId = $item->getItemId();
                            $item->isDeleted(true);
                            $this->_cart->save();
                        }
                    } else {
                    }
                }
            } else {
                $product2 = $observer->getProduct();
                if ($product2->getTypeId() == "configurable") {
                    $configClass = $product2->getTypeInstance();
                    $childProduct = $configClass->getProductByAttributes($info('super_attribute'), $product2);
                    $productIdd = $childProduct->getId();
                    //the above id is the correct id of the simple product
                    $c = $this->_cart;
                    $childProduct->isDeleted(true);
                    $c->removeItem($childProduct->getId())->save();
                    $this->_cart->save();
                }
            }
        }
    }
}

its the code below this condition

if ($product2->getTypeId() == “configurable”) {

condition thats not working, the simple products above it successfully remove from cart.