A transaction, basically, ensures that all the operations performed between the beginning and the end of the transaction are either sucessfully performed, or cancelled. So the state of the database either stays as it was before the beginning of a transaction (in case of a rollback), or contains all the modifications (insertions, updates, removals) done during the transaction (in case of a successful commit).
So just open a transaction, do all the operations you want, and commit the transaction. In case of any exception, rollback the transaction. The state of the database will stay unmodified, or will contain all the removals and reinsertions you made during the transaction.
Given that you don't really care about the reinsertions, you could also have a first transaction to remove everything and, if this transaction has successfully committed, one or several subsequent transactions for the operations and reinsertions.
The typical way of starting, committing and rollbacking transactions, in a non-managed environment (which I assume you're in), is described in the documentation.