In this paper we study generalized weights as an algebraic invariant of a code. We first describe anticodes in the Hamming and in the rank metric, proving in particular that optimal anticodes in the rank metric coincide with Frobenius-closed spaces. Then we characterize both generalized Hamming and rank weights of a code in terms of the intersection of the code with optimal anticodes in the respective metrics. Inspired by this description, we propose a new algebraic invariant, which we call "Delsarte generalized weights", for Delsarte rank-metric codes based on optimal anticodes of matrices. We show that our invariant refines the generalized rank weights for Gabidulin codes proposed by Kurihara, Matsumoto and Uyematsu, and establish a series of properties of Delsarte generalized weights. In particular, we characterize Delsarte optimal codes and anticodes in terms of their generalized weights. We also present a duality theory for the new algebraic invariant, proving that the Delsarte generalized weights of a code completely determine the Delsarte generalized weights of the dual code. Our results extend the theory of generalized rank weights for Gabidulin codes. Finally, we prove the analogue for Gabidulin codes of a theorem of Wei, proving that their generalized rank weights characterize the worst-case security drops of a Gabidulin rank-metric code.