The program crashes because when you are calling txn.Commit(), you have an iterator on the same transaction that you are using for the for loop. You cannot commit a transaction while an iterator is not yet closed.
What you probably want to do instead is create a separate txn for iterator over the data in badger and another (and more) transactions for deleting data into badger. We also have a WriteBatch API that you could use instead to achieve the same result. badger package - github.com/dgraph-io/badger/v3 - Go Packages