In the present study, the performance of two ionic liquids namely Tetrabutylphosphonium bromide (TBPB) and Tetraoctylphosphonium bromide (TOPB) as the demulsifying compounds are investigated in the demulsification operation of water in light and medium crude oil emulsions for the first time. The response surface methodology (RSM) is employed to evaluate the impact of temperature, pH, water content, demulsifier concentration, and settling time on the demulsification efficiencies of studied demulsifiers. In order to design the requisite experiments and optimize the effective factors on the water removal efficiency, the central composite design (CCD) is applied. To determine the optimal conditions of input parameters, four exact models are created by utilizing the responses of 184 experiments to predict the demulsification efficiencies of two demulsifiers for two types of crude oils based on statistical tests of different models using the analysis of variance (ANOVA). High R2 coefficient values imply that the developed models could reproduce the experimental results of the studied demulsifiers appropriately. At the optimal conditions, the water in light and medium crude oil emulsions are completely broken in the samples containing TOPB. Moreover, TBPB agent presents high and acceptable demulsification efficiencies for both types of crude oils at the optimal values of process parameters i.e. 98.078% and 96.025% for light and medium crude oil emulsions, respectively. The superior demulsification performance of TOPB with respect to TBPB can be ascribed to the longer alkyl chain length of TOPB.