To support neural circuit stability and memory function, new synapses must be selectively pruned or maintained. Li et al. used two-photon imaging of the dendritic spines of mouse primary motor cortex pyramidal neurons to demonstrate the role of rapid eye movement (REM) sleep in these processes. They found that REM sleep deprivation impairs the pruning of new synapses and the strengthening and survival of maintained new synapses during development and after motor learning, and demonstrated a role for NMDA receptor-mediated dendritic Ca2+ transients in this effect.