I always tell my students that "cancel" is not a mathematical operation.
You just saw how you can get in trouble with it above.
Consider these examples:
5/5 = 1, x/x = 1 except if x is zero x/x is undefined, (x-1)/(x-1) = 1 except when x = 1 and then (x-1)/(x-1) is undefined,
So we "cancel" a number over itself and get 1 except when we don't.
5 - 5 = 0, x - x = 0, so we "cancel" a number minus itself and get zero.
$ln(e^x) = x$ except if $x \leq 0$ and then it is undefined, so we "cancel" the exponent and the logarithm and get x except when we don't.
$^\sqrt{(9 - x^2)^2} ) = 9 - x^2$ so we "cancel" the square and square root except oops!, if x is above 3 or below negative 3 whoops, the sign is wrong so wait a minute, we have to do a correction involving absolute value...
So does "cancel" mean cross out and get zero, cross out and get 1, cross out and get x, cross out and get some function inside the arentheses except maybe we need an absolute value, and does it fail for zero or one or positives or negatives or numbers above 3 and below -3, .....
Sorry, this is a shorthand that is way more trouble than it is worth.
I strongly recommend to my students to write and speak in math, and that means using mathematical operations like divide, subtract, logarithm, and when reversing mathematical operations use and speak of the concept of inverse functions. It saves a lot of time and trouble in the long run.