Green environments are associated with improved child brain development and mental health. We study cross-sectionally the association of the availability of greenspace at home and school with obsessive-compulsive behaviors (OCB) in primary schoolchildren. Greenspace and tree cover surrounding home and school of 378 children aged 9 to 10 in Barcelona (Spain) were characterized using satellite-based indices [Normalized Difference Vegetation Index (NDVI), Modified Soil Adjusted Vegetation Index (MSAVI), Vegetation Continuous Field (VCF)] across buffers of 100, 300, and 500 m, and distance to the nearest green space. OCB was assessed with the Spence Children’s Anxiety Scale-Parent version. Linear and mixed effects models showed that greenspace at school, but not at home, was significantly related to a reduction in OCB across buffers, with benefits for girls and also children with graduate parents. Higher greenspace around the school might be associated with less obsessive-compulsive behavior in primary schoolchildren, especially in girls and those with higher socioeconomic status.