Inverter nonlinearities and machine spatial harmonics yield unwanted machine current harmonics, causing control loop instabilities, additional losses and torque ripples. In this paper a method is presented that allows online identification and compensation of both effects for anisotropic permanent magnet synchronous machines with nonlinear magnetics. The method requires no additional sensors and only fundamental component machine model parameters. It can be implemented easily in existing inverter systems by software updates. Test bench measurements show significant improvements in the whole operational area. Measurements at nominal operation result in a motor current total harmonic distortion of 0.28 % which is less than a seventh of the uncompensated value. The functional principle is transferable to other machine types or grid applications enabling the mitigation of current harmonics in a wide field of applications.