Membrane-based nanofiltration has the potential to revolutionize the large-scale treatment of organic solvents in various applications. However, the widely used commercial membranes suffer from low permeability, narrow structural tunability, and limited chemical resistance. Here, we report a strategy for fabricating covalent organic framework (COF) membranes with solvent-responsive structural flexibility. The interlayer shifting of these COF membranes in polar organic solvents results in sub-nanopores with high selectivity. High rejection rates (>99%), high permeance (>15 kilogram meter-2 hour-1 bar-1), and excellent organic solvent resistance of these smart COF membranes are achieved for a diverse array of nanofiltration applications.