Bit recycling aims at improving the rates achieved by compression techniques, such as LZ77, that suffer from the redundancy caused by the multiplicity of the encodings. The performance of bit recycling depends crucially on the recycling codes that it uses. A recipe for the construction of optimal recycling codes has been mentioned in previous work. However, no efficient algorithm and proof of optimality were given. We present both here.