People always say that boundary terms don't change the equation of motion, and some people say that boundary terms do matter in some cases. I always get confused. Here I want to consider a specific case: classical scalar field.
The action for the free scalar field is (no mass term since it's not relevant to the question) $$ S = \int \phi \partial_{\mu}\partial^{\mu}\phi \mathrm{d}^4x.\tag{1} $$ We can use Stoke's theorem to extract a so-called total derivative: $$ S = \int \phi \partial^{\mu}\phi n_{\mu} \sqrt{|\gamma|} \mathrm{d}^3x -\int\partial_{\mu}\phi \partial^{\mu}\phi \mathrm{d}^4x\tag{2} $$ where $n_{\mu}$ is the normal vector to the integral 3D hypersurface and $\gamma$ is the induced metric of the hypersurface.
The second term is another common form of scalar field. But I don't think that the first term doesn't matter at all. Variation of first term should be $$ \begin{aligned} \delta \int \phi \partial^{\mu}\phi n_{\mu} \sqrt{|\gamma|} \mathrm{d}^3x &= \int (\delta\phi) \partial^{\mu}\phi n_{\mu} \sqrt{|\gamma|} \mathrm{d}^3x \\ &+ \int \phi (\partial^{\mu} \delta\phi) n_{\mu} \sqrt{|\gamma|} \mathrm{d}^3x\\ &= \int \phi (\partial^{\mu} \delta\phi) n_{\mu} \sqrt{|\gamma|} \mathrm{d}^3x \end{aligned}\tag{3} $$ where the first term vanishes because $\delta\phi \equiv 0$ on the boundary.
But then what? There is no guarantee that $\delta \partial_{\mu} \phi$ also vanishes on the boundary. (Or there is?)
My questions are:
How to deal with this term?
More generally, what's the exact statement about the total derivative term in an action?