How does cls work under the hood? (windows)

Native Windows programs use the Console API to interact with console windows (although it is also slowly getting ANSI/xterm terminal protocol support in Windows 10).

There is Microsoft documentation page Clearing the Screen which describes how to clear a console:

  1. Use GetConsoleScreenBufferInfo to get buffer information;
  2. Use FillConsoleOutputCharacter to fill the buffer with spaces;
  3. Use FillConsoleOutputAttribute to reset the colors;
  4. Use SetConsoleCursorPosition to move the cursor to (0, 0).