We study the optimal lockdown policy for a planner who wants to control the fatalities of a pandemic while minimizing the output costs of the lockdown. We use the SIR epidemiology model and a linear economy to formalize the planners dynamic control problem. The optimal policy depends on the fraction of infected and susceptible in the population. We parametrize the model using data on the COVID-19 pandemic and the economic breadth of the lockdown. The quantitative analysis identifies the features that shape the intensity and duration of the optimal lockdown policy. Our baseline parametrization is conditional on a 1% of infected agents at the outbreak, no cure for the disease, and the possibility of testing. The optimal policy prescribes a severe lockdown beginning two weeks after the outbreak, covers 60% of the population after a month, and is gradually withdrawn covering 20% of the population after 3 months. The intensity of the lockdown depends on the gradient of the fatality rate as a function of the infected, and on the assumed value of a statistical life. The absence of testing increases the economic costs of the lockdown, leads to worse welfare outcomes and shortens the duration of the optimal lockdown.