WooCommerce programmatically deleting from cart

Use proper ajax method of wordpress like this: This worked fine for me.

//functions.php

    function remove_item_from_cart() {
    $cart = WC()->instance()->cart;
    $id = $_POST['product_id'];
    $cart_id = $cart->generate_cart_id($id);
    $cart_item_id = $cart->find_product_in_cart($cart_id);

    if($cart_item_id){
       $cart->set_quantity($cart_item_id, 0);
       return true;
    } 
    return false;
    }

    add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
    add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');

//main.js

    $.ajax({
        type: "POST",
        url: 'http://localhost/your_site/wp-admin/admin-ajax.php',
        data: {action : 'remove_item_from_cart','product_id' : '4'},
        success: function (res) {
            if (res) {
                alert('Removed Successfully');
            }
        }
    });

This seems to work.

HTML:

<?php
  foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    ?<
       <button class="remove-item" data-cart-item-key="<?=$cart_item_key;?>">
          remove item
       </button>
    <?
  }
?>

Javascript:

$('.remove-item').click(function(e){
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: ajaxurl,
        data: {
                action: 'remove_item_from_cart', 
               'cart_item_key': String($(this).data('cart-item-key'))
               }
    });
});

In functions.php, inside the template folder:

function remove_item_from_cart() {
    $cart_item_key = $_POST['cart_item_key'];
    if($cart_item_key){
       WC()->cart->remove_cart_item($cart_item_key);
       return true;
    } 
    return false;
}
add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');