The roof collapse of deep cavities has been investigated only under fully dry or saturated conditions. However, natural soils are largely unsaturated, and consequently, their mechanical behavior is significantly different owing to the presence of matric suction. In this study, a novel analytical solution for the roof collapse of deep cavities in unsaturated soils is developed based on the upper bound theorem and the nonlinear Mohr-Coulomb criterion, and the width of a cavity with an arbitrary shape at collapse is derived. Subsequently, a parametric study is conducted to investigate the effects of water retention properties, effective cohesion, friction angle, supporting pressure, and nonlinear coefficient on the safety factors of rectangular deep cavities. It is found that matric suction and soil type have significant influences on the stability of deep cavities. There is a peak for the safety factors of deep cavities as the matric suction increases when the waterholding capacity of the soil is low. This novel analytical solution provides a new insight into the stability of deep cavities in unsaturated soils.