How does the boundary term matter in scalar field and in more general cases?

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:

1. How to deal with this term?

2. More generally, what's the exact statement about the total derivative term in an action?

2. Adding boundary terms (BTs) to the action $$S$$ can affect the existence of the functional derivative. However if it still exists, it is unchanged, cf. e.g. this related Phys.SE post.
3. Concerning OP's scalar example: OP imposes Dirichlet BCs. This is appropriate for the 1st-order action $$S=-\int\!\mathrm{d}^4x\partial_{\mu}\phi \partial^{\mu}\phi,$$ but is not enough for OP's 2nd-order action (1), which requires extra BCs. Since extra BCs are likely unphysical/overconstraining, the action principle (1) is ill-posed.
• 1. So when people say that the two actions in my question are "equivalent", they actually mean that the action $S=-\int \mathrm{d}^4x \partial_{\mu} \phi \partial^{\mu}\phi$ with Dirichlet B.C. and the action $S=\int \mathrm{d}^4 x\phi \partial_{\mu}\partial^{\mu} \phi$ with Neumann B.C. give the same equation of motion in the bulk. Is this understanding correct? Commented May 9, 2022 at 2:10
• 2. Oh, I understand another thing as well. In classical mechanics (particle dynamics) we say that the variation of the coordinates vanishes at the initial and final moment: $\delta q(t_1)=\delta q(t_2)=0$. That is in fact a Dirichlet B.C.! And the reason that we only assume this B.C. but not Neumann B.C. is Lagraigans usually don't contain derivatives higher than first order, e.g. $L=\frac{1}{2} \dot{q}^2-V(q)$. Commented May 9, 2022 at 2:22