In flying ad hoc networks (FANETs), unmanned aerial vehicles (UAVs) communicate with each other without any fixed infrastructure. Because of frequent topological changes, instability of wireless communication, three-dimensional movement of UAVs, and limited resources, especially energy, FANETs deal with many challenges, especially the instability of UAV swarms. One solution to address these problems is clustering because it maintains network performance and increases scalability. In this paper, a dynamic clustering scheme based on fire hawk optimizer (DCFH) is proposed for FANETs. In DCFH, each cluster head calculates the period of hello messages in its cluster based on its velocity. Then, a fire hawk optimizer (FHO)-based dynamic clustering operation is carried out to determine the role of each UAV (cluster head (CH) or cluster member (CM)) in the network. To calculate the fitness value of each fire hawk, a fitness function is suggested based on four elements, namely the balance of energy consumption, the number of isolated clusters, the distribution of CHs, and the neighbor degree. To improve cluster stability, each CH manages the movement of its CMs and adjusts it based on its movement in the network. In the last phase, DCFH defines a greedy routing process to determine the next-hop node based on a score, which consists of distance between CHs, energy, and buffer capacity. Finally, DCFH is simulated using the network simulator version 2 (NS2), and its performance is compared with three methods, including the mobility-based weighted cluster routing scheme (MWCRSF), the dynamic clustering mechanism (DCM), and the Grey wolf optimization (GWO)-based clustering protocol. The simulation results show that DCFH well manages the number of clusters in the network. It improves the cluster construction time (about 55.51%), cluster lifetime (approximately 11.13%), energy consumption (about 15.16%), network lifetime (about 2.6%), throughput (approximately 3.9%), packet delivery rate (about 0.61%), and delay (approximately 14.29%). However, its overhead is approximately 8.72% more than MWCRSF.