to do something by accident, to finish
Expresses that an action was completed, often with a sense of regret, or that something happened unintentionally.
This pattern has two main uses. One is to express that an action is completed or finished, often with a nuance of finality. The other, and more common in daily conversation, is to express that something happened unintentionally, accidentally, or regrettably—something you didn't mean to do or wish hadn't happened. For example, if you spill coffee on your shirt, you'd say コーヒーをこぼしてしまった, conveying your misfortune. It can also be used when you finish something you were reluctant to do, like eating a whole cake when you were on a diet.