Abstract The critical ideals of a graph are the determinantal ideals of the generalized Laplacian matrix associated to a graph. A basic property of the critical ideals of graphs asserts that the graphs with at most k trivial critical ideals, Γ≤k, are closed under induced subgraphs. In this article we find the set of minimal forbidden subgraphs for Γ≤2, and we use this forbidden subgraphs to get a classification of the graphs in Γ≤2. As a consequence we give a classification of the simple graphs whose critical group has two invariant factors equal to one. At the end of this article we give two infinite families of forbidden subgraphs.