You can cancel your visa 21 days before the visa cancellation letter by giving immigration the visa cancellation letter.
The immigration will put your last date of stay until the visa cancellation date. But, you can exit Thailand before the visa cancellation date too and if you want you can come back on a tourist visa back in just a day.