How to clear downlink queue programmatically?

The network doesn’t know about the device not receiving the message. After the uplink the downlink will be sent regardless of the device state. Unless you are using ACKed downlinks which is a very bad idea by itself (search the forum if you want to know why) the network considers the downlink sent.
For V3 this will ‘fail’ if the downlink arrives after the 5 second RX window. At that point it will be scheduled for another uplink.