Nullify (zero out, cancel) rotation in an arbitrary axis in a Quaternion Question:
How do you nullify (zero out) rotation around an arbitrary axis in a Quaternion?
Example:
Let's say you have an object with quaternion orientation $A$.
You also have a rotation quaternion $B$.
You can calculate a new quaternion orientation $C=BA$.
Let's say the new orientation $C$, if done with axis-angle rotations, is equivalent to a 45 degree rotation around the arrow's local y-axis, followed by a 45 degree rotation around the arrow's local x-axis (which was moved due to the rotation around the y-axis).
In other words, imagine our 3D coordinate frame as a cube. Now imagine the initial orientation $A$ as an arrow in the center of our cube, facing in the negative-z direction. It's pointing at the center of the face of the cube whose normal is in the negative-z direction (with all normals pointing out of the cube).
Now, if we rotate this arrow 45 degrees around its local y-axis, it would be pointing at an edge of the cube. If we rotate it again, this time around its local x-axis, it would then be pointing at a corner of the cube.
The rotations described above can be encoded in a single Quaternion. A specific example of my question is, how can we alter that Quaternion so that it zeros out the rotation around, say, the x-axis, leaving only the rotation around the y-axis (meaning, in the end it would be pointing at an edge of the cube, not the corner)?
My full question is, how can we alter a Quaternion so that it zeros out the rotation around an arbitrary axis?
Remember of course that in general we would not know how the Quaternion was made, meaning we wouldn't know that it represents $a$ degrees around the x-axis, $b$ degrees around the y-axis, and $c$ degrees around the z-axis.
 A: The simplest way to model 3D rotations with quaternions is to view 3D space as the "imaginary quaternions", that is, use $i,j,k$ vectors like you do in physics to model 3D space. (But no real part.)
Every quaternion with norm 1 produces a rotation of this 3D space by conjugation $x\mapsto qxq^{-1}$, where $q$ is any length 1 quaternion.
The formula is surprisingly simple: pick a vector that points along the axis you want to rotate around, and scale it down to a unit vector, call it $h$. Then, take your angle of rotation $\theta$ and form $q=\cos(\theta/2)+h\sin(\theta/2)$. The result is a unit length quaternion that produces the rotation you wanted. ($\theta$ may be positive or negative, depending on the direction of rotation.)
I'm not sure I understand what you mean by "nullify rotation in the x and y axes", but I was guessing you meant you would like to be able to pick which axis you are rotating around.
A: From the comments to the other answer: Example: We have a sphere with a dot painted on the surface. A ray from the center of the sphere through the dot points to some fixed point $p$ and defines our axis $r$. We have a Quaternion $q$ that when used to rotate the sphere causes the dot to no longer point at $p$. The question is, how do we modify the Quat $q$ so that the dot still points at $p$? How do we restrict rotation to the axis $r$?
With this statement of your question, I think I can provide a better answer than the other one. 
Abusing notation, let $p$ be the position vector of the unrotated dot, and let $p'$ be the position vector of the dot after it has been rotated by $q$. After the rotation $q$ has been performed, the dot will usually not be pointing at $p$ (unless it was on the axis of $q$ to begin with.) Thus $p$ and $p'$ define a plane, for which we can compute a unit normal $h$ and an angle $\theta$ between $p$ and $p'$. When choosing the direction of $h$, do it in such a way that $p',p,h$ forms a right hand system, so that rotating $\theta$ in the plane would carry $p'$ onto $p$.
So we have a new quaternion rotation $z=\cos(\theta/2)+\sin(\theta/2)h$ which performs the last rotation, carrying $p'$ onto $p$. Composing the two rotations into $zq$, you would have a rotation with axis $r$, and so the dot isn't moved.
Finally, it is important to note that if $p$ is perpendicular to the axis of $q$, then $z$ is just going to be $q^{-1}$ in this scheme, and then $zq$ would be the identity. This seems to fit into your question, because in that case the $r$ axis is not experiencing any "torque" during the rotation.

Here's my shot at answering the original question based on the new information. Again, let $p'=qpq^{-1}$, and let $\pi$ be the plane normal to $r$, the direction that you want to cancel rotation in. Project both $p$ and $p'$ into $\pi$, and measure the angle $\theta$ between. Normalize $r$ to $h$ and compute $z$ as before, and then the rotation $zq$ should remove the twist, as you hoped.
The only problem with this is that there are edge cases when $p$ or $p'$ is parallel to $r$, so that the projections don't form vectors. I'll have to think about these tomorrow.
A: @rschwieb
I believe I've come up with an answer to the original question based on rschwieb's comments and his solution to a similar problem.
Let's say we have some unit direction vector $r$ that defines the axis in which we want no rotation to occur. Pick any unit direction vector $p$ that is perpendicular to $r$.
We also have the Quaternion rotation $q$ from which we want to nullify any rotation in the axis $r$. Rotate $p$ by $q$, calculating $p'=qpq^{*}$ (take care here).
Now, create a plane defined by our axis $r$. Project $p'$ onto that plane creating a new vector $p''$. Be sure to normalize $p''$.
Then, calculate the angle $\theta$ between $p$ and $p''$. $\theta$ is the angle of rotation in the axis $r$ that we want to remove. So, create a Quaternion $z=[cos(\theta/2),sin(\theta/2)r_x,sin(\theta/2)r_y,sin(\theta/2)r_z]$. Be sure to use the correct sign of $\theta$.
Finally, create a new Quaternion $w=zq$. When $w$ is used to rotate an object instead of $q$, no rotation will occur around the axis $r$.
How does that sound?
A: This is the same approach, but in more direct terms:  To find the quaternion that is closest to $q$, but has no component in the direction of your arbitrary axis $v$, first rotate $v$ by $q$ to get $w$
$$w = R(q)^\top v.$$
Then, find the shortest rotation $\hat{q}$ between $w$ and $v$.  Being the shortest rotation, $\hat{q}$ by definition has no rotation about either $v$ or $w$. Its angle is $\theta=\arccos\left({v^\top w}\right)$ and the axis is $\delta = \tfrac{v \times w}{\lVert v\times w\rVert}$.
$$\hat{q} = \pmatrix{\cos\left(\frac{\theta}{2}\right) \\ \sin\left(\frac{\theta}{2}\right)\delta}$$.
