Aims. The aim of this study was to determine the prevalence of depressive and anxiety disorders prior to total hip (THA) and total knee arthroplasty (TKA) and to assess their impact on the rates of any infection, revision, or reoperation. Methods. Between January 2000 and March 2019, 21,469 primary and revision arthroplasties (10,011 THAs; 11,458 TKAs), which were undertaken in 15,504 patients at a single academic medical centre, were identified from a 27-county linked electronic medical record (EMR) system. Depressive and anxiety disorders were identified by diagnoses in the EMR or by using a natural language processing program with subsequent validation from review of the medical records. Patients with mental health diagnoses other than anxiety or depression were excluded. Results. Depressive and/or anxiety disorders were common before THA and TKA, with a prevalence of 30% in those who underwent primary THA, 33% in those who underwent revision THA, 32% in those who underwent primary TKA, and 35% in those who underwent revision TKA. The presence of depressive or anxiety disorders was associated with a significantly increased risk of any infection (primary THA, hazard ratio (HR) 1.5; revision THA, HR 1.9; primary TKA, HR 1.6; revision TKA, HR 1.8), revision (THA, HR 1.7; TKA, HR 1.6), re-revision (THA, HR 2.0; TKA, HR 1.6), and reoperation (primary THA, HR 1.6; revision THA, HR 2.2; primary TKA, HR 1.4; revision TKA, HR 1.9; p < 0.03 for all). Patients with preoperative depressive and/or anxiety disorders were significantly less likely to report “much better” joint function after primary THA (78% vs 87%) and primary TKA (86% vs 90%) compared with those without these disorders at two years postoperatively (p < 0.001 for all). Conclusion. The presence of depressive or anxiety disorders prior to primary or revision THA and TKA is common, and associated with a significantly higher risk of infection, revision, reoperation, and dissatisfaction. This topic deserves further study, and surgeons may consider mental health optimization to be of similar importance to preoperative variables such as diabetic control, prior to arthroplasty. Cite this article: Bone Joint J 2023;105-B(5):526–533