Fix replace method: deallocate before allocate.

When same ids are given it was not behaving correctly. It was deleting newly created circuits.