Quoting from [a paper][1] by Roth and Vakil ($\mathrm{cd}$ stands for cohomological dimension): > Let $S$ be $\mathbb{P}^2$ blown up at a point, and let $X$ be the > affine cone over some projective embedding of $S$. Let $Z\subset X$ be > the affine cone over the exceptional divisor of the blowup. Then $Z$ > is of codimension one in $X$, but $\mathrm{cd}(X\backslash Z) = 1$, so > in particular it is not affine. This shows that, conversely, the > complement of a Weil divisor in an affine scheme need not be affine... Note that the complement of an effective Cartier divisor in an irreducible affine scheme *is* affine so if in addition to the assumptions in the question the ambient scheme is factorial ([e.g. regular][2]), then the complement of any pure codimension 1 closed subset is affine. [1]: https://arxiv.org/abs/math/0406384 [2]: https://justinsmath.wordpress.com/2014/09/23/regular-implies-locally-factorial/