Abstract. Recently, it has been shown that flow blockage in large wind farms may lift up the top of the boundary layer, thereby triggering atmospheric gravity waves in the inversion layer and in the free atmosphere. These waves impose significant pressure gradients in the boundary layer, causing detrimental consequences in terms of a farm's efficiency. In the current study, we investigate the idea of controlling the wind farm in order to mitigate the efficiency drop due to wind-farm-induced gravity waves and blockage. The analysis is performed using a fast boundary layer model which divides the vertical structure of the atmosphere into three layers. The wind-farm drag force is applied over the whole wind-farm area in the lowest layer and is directly proportional to the wind-farm thrust set-point distribution. We implement an optimization model in order to derive the thrust-coefficient distribution, which maximizes the wind-farm energy extraction. We use a continuous adjoint method to efficiently compute gradients for the optimization algorithm, which is based on a quasi-Newton method. Power gains are evaluated with respect to a reference thrust-coefficient distribution based on the Betz–Joukowsky set point. We consider thrust coefficients that can change in space, as well as in time, i.e. considering time-periodic signals. However, in all our optimization results, we find that optimal thrust-coefficient distributions are steady; any time-periodic distribution is less optimal. The (steady) optimal thrust-coefficient distribution is inversely related to the vertical displacement of the boundary layer. Hence, it assumes a sinusoidal behaviour in the streamwise direction in subcritical flow conditions, whereas it becomes a U-shaped curve when the flow is supercritical. The sensitivity of the power gain to the atmospheric state is studied using the developed optimization tool for almost 2000 different atmospheric states. Overall, power gains above 4 % were observed for 77 % of the cases with peaks up to 14 % for weakly stratified atmospheres in critical flow regimes.