At least in the USA there are options for alternative schooling and homeschooling, which allow exceptions to compulsory public school attendance. Look to Europe though and you'll find places where it's absolutely illegal to do anything but go through the child brainwashing factories. Germany is a good example of this.
I'm fairly certain (at least for the US) it has been illegal. "By 1918, every state had a compulsory attendance law on the books." [1]
According to this table, school attendance is compulsory for every state in the US for specific age ranges: https://nces.ed.gov/programs/statereform/tab5_1.asp
[1] https://hslda.org/content/docs/nche/Issues/S/State_Compulsor...