Microglia are the main immune cells of the brain and contribute to common brain diseases. However, it is unclear how microglia influence neuronal activity and survival in the injured brain in vivo. Here we develop a precisely controlled model of brain injury induced by cerebral ischaemia combined with fast in vivo two-photon calcium imaging and selective microglial manipulation. We show that selective elimination of microglia leads to a striking, 60% increase in infarct size, which is reversed by microglial repopulation. Microglia-mediated protection includes reduction of excitotoxic injury, since an absence of microglia leads to dysregulated neuronal calcium responses, calcium overload and increased neuronal death. Furthermore, the incidence of spreading depolarization (SD) is markedly reduced in the absence of microglia. Thus, microglia are involved in changes in neuronal network activity and SD after brain injury in vivo that could have important implications for common brain diseases.