TITLE: Generalization of the statement that $\mathbb{A}^2\setminus (0,0)$ is not affine?
QUESTION [7 upvotes]: $\mathbb{A}^2\setminus (0,0)$ is often given as an example of a variety that is not affine. I am trying to understand this example better by seeing it as a special case of a natural general claim.
Claim: Let $X$ be an affine variety over an algebraically closed field $k$ such that $k[X]$ is a UFD. Let $Y$ be any closed subset of codimension at least 2.  Then $X\setminus Y$ is not affine.

Is this the right general claim?

If so, can I drop the assumption that $k$ is algebraically closed, or is that needed?

REPLY [7 votes]: I think the right generalization to you your example is the following.

Let $X$ be an integral affine, Notherian normal scheme, and $F$ a (EDIT: Non-empty--thanks Cantlog) closed subset with $\text{codim}_X(F)\geqslant 2$. Then, $X-F$ is a non-affine open subscheme.

Indeed, using "Algebraic Hartog's Lemma" one can show that the inclusion $(X-F)\hookrightarrow X$ induces an isomorphism $\mathcal{O}_X(X)\to\mathcal\to\mathcal{O}_{X}(X-F)$. If $X-F$ were affine, this would contradict the equivalence of categories $\mathsf{Sch}^{\text{op}}\xrightarrow{\approx}\mathsf{Ring}$.