We show a sharp conformally invariant gap theorem for Yang-Mills connections in dimension 4 by exploiting an associated Yamabe-type problem.