The advent of efficient numerical algorithms for the construction of one-loop amplitudes has played a crucial role in the automation of NLO calculations, and the development of similar algorithms at two loops is a natural strategy for NNLO automation. Within a numerical framework the numerator of loop integrals is usually constructed in four dimensions, and the missing rational terms, which arise from the interplay of the (D − 4)-dimensional parts of the loop numerator with 1/(D − 4) poles in D dimensions, are reconstructed separately. At one loop, such rational terms arise only from UV divergences and can be restored through process-independent local counterterms. In this paper we investigate the behaviour of rational terms of UV origin at two loops. The main result is a general formula that combines the subtraction of UV poles with the reconstruction of the associated rational parts at two loops. This formula has the same structure as the R-operation, and all poles and rational parts are described through a finite set of process-independent local counterterms. We also present a general formula for the calculation of all relevant two-loop rational counterterms in any renormalisable theory based on one-scale tadpole integrals. As a first application, we derive the full set of two-loop rational counterterms for QED in the Rξ -gauge.