We show that for generic Riemannian metrics on a simply-connected closed spin manifold of dimension ≧5 the dimension of the space of harmonic spinors is not larger than it must be by the index theorem. The same result holds for periodic fundamental groups of odd order. The proof is based on a surgery theorem for the Dirac spectrum which says that if one performs surgery of codimension ≧3 on a closed Riemannian spin manifold, then the Dirac spectrum changes arbitrarily little provided the metric on the manifold after surgery is chosen properly.