Refrigerators use a thermodynamic cycle to move thermal energy from a cold reservoir to a hot one. Implementing this operation principle with mesoscopic components has recently emerged as a promising strategy to control heat currents in micro and nanosystems for quantum technological applications. Here we combine concepts from stochastic and quantum thermodynamics with advanced methods of optimal control theory to develop a universal optimization scheme for such small-scale refrigerators. Covering both the classical and the quantum regime, our theoretical framework provides a rigorous procedure to determine the periodic driving protocols that maximize either cooling power or efficiency. As a main technical tool, we decompose the cooling cycle into two strokes, which can be optimized one by one. In the regimes of slow or fast driving, we show how this procedure can be simplified significantly by invoking suitable approximations. To demonstrate the practical viability of our scheme, we determine the exact optimal driving protocols for a quantum microcooler, which can be realized experimentally with current technology. Our work provides a powerful tool to develop optimal design strategies for engineered cooling devices and it creates a versatile framework for theoretical investigations exploring the fundamental performance limits of mesoscopic thermal machines.