Duplicate key on delete and update of unique key
The mentioned issue will not surface for the given code. If you could create a new code with which we shall reproduce the same error, immediate reply is guaranteed.

The choices are


Do not alter the unique key. You shall redesign your logic.
Unique key fields takes little longer to get updated as they need to be updated both in the data file and the index file. So, give some time gap before using the same key.
Try (atleast) committing the delete and then move on to creating another row with the same deleted key row (though not a good suggestion).