Ground collapse occurs frequently due to the roof or roof–sides breakdown of shallow cavities, but analytical solutions considering both types of failures remains a challenge and without incorporating the unsaturated characteristics of shallow soils. This study presents an analytical solution for the collapse of shallow rectangular cavities in unsaturated soils. Proposing a critical cover depth to differentiate deep and shallow cavities. Results show that the collapse type (roof or roof–sides) in shallow cavities depends on the width at collapse. It varies significantly with matric suction, displaying two patterns: increasing and stabilizing, or increasing, decreasing and stabilizing. The variation trend of width at collapse with matric suction aligns with that of apparent cohesion, with a peak near the air-entry value when the fitting parameter n is relatively large (such as n = 3 or 4). The air-entry value influences the magnitude rather than the shape of the variation curves. Additionally, shallow cavities with small cover depths tend toward roof failure, while those with large cover depths, especially with significant height, are more prone to roof–sides failure. Increasing surface surcharge can reduce collapse width, shifting towards roof–sides failure, especially with low cohesion or internal friction angle.