Trouble canceling a take profit order, and stop loss order of a bracket order

I am using tradingview webhook alerts to signal a python program being run on aws (thanks larry) to execute specific orders.

The goal is to cancel whatever remaining sell orders there are for a specific asset, take profit and stop loss, and to execute a new limit sell order of all but one share that I own.

request = app.current_request
    webhook_message = request.json_body

    api = tradeapi.REST(API_KEY, SECRET_KEY, base_url=BASE_URL)
    ids = []

    orders = api.list_orders(
    status = 'open',
    nested=True  # show nested multi-leg orders
    ticker_orders = [o for o in orders if o.symbol == webhook_message['ticker']]
    for i in ticker_orders:
        if len(orders)>0:
            for i in ids:
                print('orders canceled')
            print('there are no orders')

    account = api.get_account()
    positions = api.list_positions()
    qty = api.get_position(webhook_message['ticker']).qty

        'message': 'I bought the stock!',
        'webhook_message': webhook_message

This is the method that I currently have. When executed, it is able to cancel remaining orders properly. The issue comes into play because it is not properly submitting the new limit order. The error message I receive when testing on a local host is, “not enough shares to execute (0 of 16)” At first I thought that this may be a result of not properly canceling all legs of a bracket order before trying to execute the updated order. (i.e. cancel the limit order but not cancel the stop order). But it just so happened that it was triggered today when there was not stop limit order queued and I was given the same error.

What is the best way to solve this problem? is canceling the orders even necessary or is there a way to place the new limit order that would immediately take trump of standing orders?

This was pretty long winded so I hope it made enough sense. Thanks in advance for reading and any help.