Context. Understanding the Sun's magnetic activity is important because of its impact on the Earth's environment. Direct observations of the sunspots since 1610 reveal an irregular activity cycle with an average period of about 11 years, which is modulated on longer timescales. Proxies of solar activity such as 14 C and 10 Be show consistently longer cycles with well-defined periodicities and varying amplitudes. Current models of solar activity assume that the origin and modulation of solar activity lie within the Sun itself; however, correlations between direct solar activity indices and planetary configurations have been reported on many occasions. Since no successful physical mechanism was suggested to explain these correlations, the possible link between planetary motion and solar activity has been largely ignored. Aims. While energy considerations clearly show that the planets cannot be the direct cause of the solar activity, it remains an open question whether the planets can perturb the operation of the solar dynamo. Here we use a 9400 year solar activity reconstruction derived from cosmogenic radionuclides to test this hypothesis. Methods. We developed a simple physical model for describing the time-dependent torque exerted by the planets on a non-spherical tachocline and compared the corresponding power spectrum with that of the reconstructed solar activity record. Results. We find an excellent agreement between the long-term cycles in proxies of solar activity and the periodicities in the planetary torque and also that some periodicities remain phase-locked over 9400 years. Conclusions. Based on these observations we put forward the idea that the long-term solar magnetic activity is modulated by planetary effects. If correct, our hypothesis has important implications for solar physics and the solar-terrestrial connection.