Most manufacturers claim that their thermal paste has a lifespan stretching between three and five years; however, these figures are calculated with the assumption that the paste is being held in optimal conditions. This may not be the case with your build.
For CPUs, a good rule of thumb is to replace the thermal paste once annually, just to ensure it’s performing at the peak of its potential. Although, there are a couple of instances that call for a slight deviation from the yearly schedule.
For example, if you’re replacing your CPU cooler, or even if you're just removing your current one to do some inspections, you’ll need to replace the thermal paste before refitting.
GPUs don’t require anywhere near as much maintenance in the thermal paste department. They tend to run for 5-10 years before it’ll need replacing, at which point you may be due an upgrade anyway.