This paper develops a flexible collision avoidance strategy for the formation of multiple unmanned aerial vehicles (multi-UAV). Firstly, To make full use of the information flow from the communication of the formation, an improved artificial potential field (IAPF) function is constructed by adding the communication topology and communication weights. Based on the IAPF functions, both collision avoidance within the formation and obstacle avoidance outside the formation are well organized to ensure flight safety. Secondly, to fuse collision avoidance and formation keeping, we design the null space behavioral (NSB) approach to combine them into a single motion command, where the formation keeping algorithm is designed as a second-order consensus algorithm. In this way, the flexible collision avoidance strategy is proposed based on IAPF and consensus, handling the relationship between collision avoidance and formation keeping flexibly. Finally, three-dimensional multi-UAV flight simulation validates the effectiveness of the proposed strategy that the formation can both implement collision avoidance quickly and keep a good geometric formation configuration simultaneously.INDEX TERMS Unmanned aerial vehicle, collision avoidance, formation keeping, improved artificial potential field, null space behavioral.