Cocaine use disorder (CUD) is a global health problem with no approved medications. One potential treatment target is the gut microbiome, but it is unknown if cocaine induces long-lasting effects on gut microbes. A novel therapeutic candidate for CUD, cannabidiol (CBD), can improve gut function in rodent models. It is possible that protective effects of CBD against cocaine use are mediated by improving gut health. We examined this question in this experiment. Cocaine conditioned place preference (CPP) was conducted in adult male C57BL/6JArc mice. Mice were treated with vehicle or 20 mg/kg CBD prior to all cocaine CPP sessions (N = 11–13/group). Mice were tested drug free 1, 14 and 28 days after cessation of cocaine and CBD treatment. Fecal samples were collected prior to drug treatment and after each test session. Gut microbiome analyses were conducted using 16 s rRNA sequencing and correlated with behavioural parameters. We found a persistent preference for a cocaine-environment in mice, and long-lasting changes to gut microbe alpha diversity. Cocaine caused persistent changes to beta diversity which lasted for 4 weeks. CBD treatment reduced cocaine-environment preference during abstinence from cocaine and returned gut beta diversity measures to control levels. CBD treatment increased the relative abundance of Firmicutes phyla and Oscillospira genus, but decreased Bacteroidetes phyla and Bacteroides acidifaciens species. Preference score in cocaine-treated mice was positively correlated with abundance of Actinobacteria, whereas in mice treated with CBD and cocaine, the preference score was negatively correlated with Tenericutes abundance. Here we show that CBD facilitates cocaine extinction memory and reverses persistent cocaine-induced changes to gut microbe diversity. Furthermore, CBD increases the abundance of gut microbes which have anti-inflammatory properties. This suggests that CBD may act via the gut to reduce the memory of cocaine reward. Our data suggest that improving gut health and using CBD could limit cocaine abuse.