If you can recompile nginx, you might consider using the module I developed.
https://github.com/HanadaLee/ngx_http_request_cookies_filter_module
location /api {
clear_request_cookie B;
proxy_set_header $filtered_request_cookies;
proxy_pass <upstream>;
}