In this paper, a task space-based methodology for dynamic trajectory planning for digging process of a hydraulic excavator is presented, with the integration of soil–bucket interaction. An extended soil–bucket interaction model, which adds the resistive moment compared to the previous models, is provided in this research. This improved model is validated by comparing with the measurement data taken from field experiments before integrating it into a dynamic model of an excavator. Further, Newton–Euler method is used for the derivation of the dynamics of each link of the excavator to determine the joint forces, which can cause the machine damage. The position and orientation trajectories of the bucket in the task space are parameterized by using the B-splines, so as to achieve the task-oriented operations and ensure the operation flexibility. The joint space motion characteristics are obtained by solving the inverse kinematics of the working mechanism of an excavator. Moreover, to avoid the operation uncertainty for a given bucket tip position trajectory and reduce the computational effort, the self-motion parameters are introduced when solving the inverse kinematics of the redundant working mechanism. All these self-motion parameters are taken as a set of design variables in the trajectory optimization problem. Also, the limits on the hydraulic driving forces, joint angles, angular velocities and accelerations, as well as bucket capacity are considered as the optimization constraints for the digging process. Finally, optimization examples of two typical digging categories (i.e. level digging work and slope digging work) are given to demonstrate and verify the capabilities of the new methodology proposed in this research. The results show that the proposed method can effectively generate the optimal trajectories satisfying the following criteria: time efficiency, energy efficiency, and least machine damage. This work lays a solid foundation for motion planning and autonomous control of an excavator.