In this paper, instantaneous reactive power theory (IRP), also known as p–q theory based a new control algorithm is proposed for 3-phase 4-wire and 4-leg shunt active power filter (APF) to suppress harmonic currents, compensate reactive power and neutral line current and balance the load currents under unbalanced non-linear load and non-ideal mains voltage conditions. The APF is composed from 4-leg voltage source inverter (VSI) with a common DC-link capacitor and hysteresis–band PWM current controller. In order to show validity of the proposed control algorithm, compared conventional p–q and p–q–r theory, four different cases such as ideal and unbalanced and balanced-distorted and unbalanced-distorted mains voltage conditions are considered and then simulated. All simulations are performed by using Matlab-Simulink Power System Blockset. The performance of the 4-leg APF with the proposed control algorithm is found considerably effective and adequate to compensate harmonics, reactive power and neutral current and balance load currents under all non-ideal mains voltage scenarios.