The textile industry uses large amounts of water that becomes wastewater contaminated with salts, starches, acids, peroxides, enzymes, dyes, and other pollutants. Here, we optimized the treatment of textile industry wastewater by electrocoagulation (EC) using aluminum and iron electrodes employing a response surface methodology (RSM) approach. Hence, the effects and interactions of the variables of the process (pH, current density, and reaction time) on turbidity and chemical oxygen demand (COD) removal were evaluated using the Box-Behnken mathematical model in a monopolar batch reactor. Analyses of variance were also performed to estimate model responses and optimum conditions. After determining the mathematically optimized variables of the EC process, its efficiency in the removal of heavy metals and cations from textile wastewater was also evaluated for both the electrodes under study. Experimental data yielded 89.92 and 86.38% in COD removal, and 99.75 and 97.67% in turbidity removal for Al and Fe electrodes. Moreover, the theoretical maximum removals are 93.58 and 87.08% of COD for Al and Fe electrodes and 100% of TB for both electrodes. The removal of U, As, V, Si, P, and Pb was above 90% with both electrode materials. Although both electrodes removed part of the cations, the aluminum electrodes yielded higher removals of Ca+2 and Na+1 (46.19 and 31.94%). Therefore, our study demonstrates that the RSM and Box-Behnken are suitable for optimizing the electrocoagulation process to treat textile wastewater and that the EC is an efficient technology for removing different organic and inorganic pollutants.