def remove_listener_by_name(target, identifier, name):
    """
    removes already registered sqlalchemy listener
    use this one only if fn pointer is not accessable (inner function)
    otherwise use regular remove from event api

    Example:
        from airflow import settings
        target = settings.engine
        remove_listener_by_name(target, "engine_connect", "ping_connection")
    """
    import ctypes
    from sqlalchemy import event
    from sqlalchemy.event.registry import _key_to_collection
    all_keys = list(event.registry._key_to_collection.items())
    for key, values in all_keys:
        if key[0] != id(target):
            continue
        if identifier != key[1]:
            continue
        fn = ctypes.cast(key[2], ctypes.py_object).value
        if fn.__name__ != name:
            continue
        event.remove(target, identifier, fn)
