Covalent organic frameworks (COFs) are attractive materials for sample pretreatment due to their tunable structures and functions. However, the precise recognition of contaminant in complex environmental matrices by COFs remains challenging owing to their insufficient specific active sites. Herein, we report Co2+ coordination-assisted molecularly imprinted flexible COF (MI-COF@Co2+) for selective recognition of ochratoxin A (OTA). The MI-COF@Co2+ was prepared via one-step polymerization of 3,3-dihydroxybenzidine, 2,4,6-tris(4-formylphenoxy)-1,3,5-triazine, Co2+ and template. The flexible units endowed COFs with the self-adaptable ability to regulate the molecular conformation and coordinate with Co2+ to locate the imprinted cavities. The coordination interaction greatly improved the adsorption capacity and selectivity of MI-COF@Co2+ for OTA. The prepared MI-COF@Co2+ was used as solid phase extraction adsorbent for high-performance liquid chromatography determination of OTA with the detection limit of 0.03 ng mL−1 and the relative standard deviation of < 2.5%. In addition, this method permitted interference-free determination of OTA in real samples with recovery from 89.5% to 102.8%. This work provides a simple way to improve the selectivity of COFs for the determination of hazardous compounds in complex environments. Ochratoxin A (OTA), a secondary metabolite of Aspergillus and Penicillium with high toxicity and carcinogenicity, seriously threatens the agricultural environment and human health. Development of a rapid and selective method for the detection of trace OTA residues in complex samples is still challenging. In this work, we report a novel Co2+ coordination-assisted molecularly imprinted flexible covalent organic framework (MI-COF@Co2+) for highly sensitive and matrix-free OTA determination in real samples.