Solar panels that don't sustain some environmental damage tend to last 50 years or more (their power output degrades, but they still function and put out useful amounts of electricity). By the time most panels need to actually be fully replaced and recycled, I think we'll have the facilities to do so.
But even if we somehow can't, and every single solar panel is destined for landfill after 50 years (which is highly unlikely), they'd still be a far better option environmentally and climate wise compared to burning gas, coal, or wood at scale.