Clearing the commit history using REST API

Let’s assume I have a branch, main, with a linear commit history i.e.

1 > 2 > 3 > 4 > [...] > 102 > 103 > 104

As part of my cleanup operation, I’d like to remove all but the latest commit from history. Or, ideally, all but the last ~10.

Is there a workflow that would work using the Database API? I can’t quite get my head around a flow which doesn’t leave me with commits without a branch!

Alternatively, is there a API call to clean up these straggling branchless commits? Will gc take care of these automatically?

TIA!