This paper studies the efficient allocation in an economy in which firms are connected through input-output linkages and must pay a fixed cost to produce. When economic conditions are poor, some firms might decide not to operate, thereby severing the links with their neighbors and changing the structure of the production network. Since producers benefit from having access to additional suppliers, nearby firms tend to operate, or not, together. As a result, the production network features clusters of operating firms, and the exit of a producer can create a cascade of firm shutdowns. While well-connected firms are better able to withstand shocks, they trigger larger cascades upon exit. The theory also predicts how the structure of the production network changes over the business cycle. As in the data, recessions are associated with more dispersed networks that feature fewer highly connected firms. In the calibrated economy, the endogenous reorganization of the network substantially dampens the impact of idiosyncratic shocks on aggregate fluctuations.