I have found two different ways of doing this and I am seeking commentary on the fine nuance. Suppose there is a Hamiltonian
$$ H=\frac{1}{2}\int\!d^3x \left[ \pi^2+(\nabla\varphi)^{\!2}+m^2\varphi^2 \right] ~~. \tag{1}$$
Hamilton's equations are
$$ \dot\pi=-\dfrac{\delta H}{\delta \varphi} \qquad\text{and}\qquad \dot\varphi=\dfrac{\delta H}{\delta \pi}~~. \tag{2} $$
It follows that
$$ \dot \varphi=\int\!d^3x\,\pi~~, \tag{3}$$
but I have a question about how to compute $\dot\pi$. If I trivially take the derivative with respect to $\varphi$ as $$\frac{\delta}{\delta\varphi}\equiv\frac{\partial}{\partial\varphi},\tag{4}$$ skipping over the gradient term, I get the wrong answer $\dot\pi=\int d^4x\, m^2\varphi$. I have found two different ways to get the correct answer. The first is to use the identity
$$ (\nabla\varphi)^2=\nabla(\varphi\nabla\varphi)-\varphi\nabla^2\varphi ~~,\tag{5}$$
to rewrite the Hamiltonian as
$$ H=\frac{1}{2}\int\!d^3x \left[ \pi^2+\nabla(\varphi\nabla\varphi)-\varphi\nabla^2\varphi +m^2\varphi^2 \right] ~~.\tag{6}$$
Now when I take the partial with respect to $\varphi$ I get the correct answer
$$ \dot \pi=\int\!d^3x\, \left[\nabla^2\varphi-m^2\varphi\right]~~. \tag{7} $$
Mainly my question is this: What rule is it that dictates that $\frac{\delta}{\delta\varphi}$ has to hit the $\nabla\varphi$ term? Something about the linearity of the operators, I am sure, but I am not certain exactly what my reasoning is. I found another way to compute the correct answer and that is also what my question is about because the other method suggests the derivative does not have to hit that term. I saw something on the internet that says Hamilton's equations for fields use the "functional derivative"
$$ \dfrac{\delta}{\delta \varphi} \equiv\dfrac{\partial}{\partial \varphi}-\nabla\dfrac{\partial}{\partial (\nabla \varphi)} ~~.\tag{8}$$
I can apply this to the original Hamiltonian as
\begin{align} \dot\pi=-\dfrac{\delta}{\delta \varphi} H&=-\left(\dfrac{\partial}{\partial \varphi}-\nabla\dfrac{\partial}{\partial (\nabla \varphi)} \right)\frac{1}{2}\int\!d^3x \left[ \pi^2+(\nabla\varphi)^{\!2}+m^2\varphi^2 \right]\\ &=-\int\!d^3x \,m^2\varphi+\int\!d^3x\, \nabla\nabla\varphi\\ &=\int\!d^3x\,\nabla^2\varphi-m^2\varphi\tag{9} \end{align}
Once I have to correct $\dot\varphi,\dot\pi$, I can easily proceed to derive the KG equations of motion. I am seeking input regarding the relative merits and uses of these two procedures for computing $\dot\pi$. Initially, I got the wrong answer because I thought $\frac{\delta}{\delta\varphi}(\nabla\varphi)=0$ and I want to better understand why I was wrong.