Dendritic spines are the major loci of synaptic plasticity and are considered as possible structural correlates of memory. Nonetheless, systematic manipulation of specific subsets of spines in the cortex has been unattainable, and thus, the link between spines and memory has been correlational. We developed a novel synaptic optoprobe, AS-PaRac1 (activated synapse targeting photoactivatable Rac1), that can label recently potentiated spines specifically, and induce the selective shrinkage of AS-PaRac1-containing spines. In vivo imaging of AS-PaRac1 revealed that a motor learning task induced substantial synaptic remodelling in a small subset of neurons. The acquired motor learning was disrupted by the optical shrinkage of the potentiated spines, whereas it was not affected by the identical manipulation of spines evoked by a distinct motor task in the same cortical region. Taken together, our results demonstrate that a newly acquired motor skill depends on the formation of a task-specific dense synaptic ensemble.