The association between retirement and mental health is assumed to depend on socio-demographic factors, but there is a lack of empirical evidence. We examined antidepressant medication before and after retirement by age, gender, living arrangements, and social class. The material comprised nationally representative register data from Finland. Linear regression analysis was used to calculate changes in antidepressant medication 7.5years before and after disability (N=42,937) and old-age (N=19,877) retirement in 1997-2007 by socio-demographic factors. No changes in antidepressant medication (mean DDD/3-month period) were observed around old-age retirement regardless of the socio-demographic factors. After a slight initial increase in antidepressant medication there was a substantial rise 1.5-0years before disability retirement, after which there was a marked decrease, particularly during the first 3years. These effects were less pronounced in retirement due to somatic causes. Age was the strongest modifying factor in retirement due to both depression and somatic causes, with a stronger increase and limited decrease in antidepressant medication among the younger age groups. The post-retirement decrease was also somewhat stronger among men, and among those in higher social classes and those living with a spouse in depression-related retirement. In somatic causes, the pre-retirement increase was stronger among those in higher social classes. Prevention and rehabilitation of mental-health problems in association with work disability should focus particularly on young adults, among whom the strong increase in antidepressant medication before disability retirement, and the continued high levels of medication after the transition signify long periods of morbidity and premature retirement.