Local galaxies are broadly divided into two main classes, star-forming (gas-rich) and quiescent (passive and gas-poor). The primary mechanism responsible for quenching star formation in galaxies and transforming them into quiescent and passive systems is still unclear. Sudden removal of gas through outflows or stripping is one of the mechanisms often proposed. An alternative mechanism is so-called ``strangulation'', in which the supply of cold gas to the galaxy is halted. Here we report an analysis of the stellar metallicity (the fraction of elements heavier than helium in stellar atmospheres) in local galaxies, from 26,000 spectra, that clearly reveals that strangulation is the primary mechanism responsible for quenching star formation, with a typical timescale of four billion years, at least for local galaxies with a stellar mass less than 1011 solar masses. This result is further supported independently by the stellar age difference between quiescent and star-forming galaxies, which indicates that quiescent galaxies of less than 1011 solar masses are on average observed four billion years after quenching due to strangulation.