How an advance works
It is a pre-payment of royalties, usually split. Signature, delivery/acceptance, and publication is the classic three. Some houses still split the last third between hardback and paperback publication — do not let a paperback that may never happen hold your money hostage. Add a long-stop date.
You keep the advance if the book does not earn out, provided you delivered an acceptable manuscript and they have not terminated for a genuine breach. You do not send them a cheque for the unearned balance. You also do not get a further royalty cheque until the unearned balance is gone.