The latent heat thermal energy storage (LTES) strengthened by metal foam can promote the development of renewable energy and waste heat recovery. In this study, superposition method and 2D iterative method are established to optimize the 2D porosity gradient of metal foam, which is infiltrated in phase change material (PCM) for heat transfer enhancement in LTES with shell-and-tube configuration. The optimization process in these new methods takes the interaction between horizontal and vertical porosity gradient into account and the evolution of two-dimensional metal foam porosity is numerically investigated. Melting behaviors of optimal cases with different methods are compared and analyzed. The numerical results demonstrated that the optimal case obtained by 2D iterative method has best heat transfer performance. Compared with the uniform distribution, a 11.32 % reduction in full melting time can be achieved by the 2D iterative optimal case due to its better temperature uniformity and stronger natural convection.