Delete Application & Transfer out Assets/Algos

I have a stateful smart contract (escrow) where algos & assets are flowing in and out.
At some point, there will be some algos and possibly some assets with balances.
I want the app creator to be able to reclaim the algos + assets that remain, and delete the app.

It seems I can delete the application but the underlying account (with funds) remains.

Looking for the correct technique to handle this - seems that:
(1) Need some inner transactions to send remaining assets, then algos, from the application (escrow) back to app creator
(2) Delete Application
(3) Then there is no way to delete the orphan account (that was the application account address) ?

Any guidance appreciated

You are right: before deleting the application, you need to empty the application account.
There is no way to recover the funds/assets after deletion of the application, if you don’t do it.

You can do it via inner transactions during the delete application transaction too, if you want.
It does not need to be a separate transaction.

To close completely the account, you need to send a “close” transaction for each asset and then for the Algos, that is you need to specify the AssetCloseTo and CloseRemainderTo fields respectively.
See Algos and assets - Algorand Developer Portal

1 Like

Here is some TEAL code that handles these scenarios, i.e. returning an asset and algos from the smart contract to the creator account (in my case). Using 0 in the amount field so that the entire remainder gets swept (Closed To)

itxn_begin
int axfer
itxn_field TypeEnum
txn Sender
itxn_field AssetReceiver
txna Assets 0
itxn_field XferAsset
pushint 0
itxn_field AssetAmount
txn Sender
itxn_field AssetCloseTo
itxn_submit

itxn_begin
int pay
itxn_field TypeEnum
pushint 0
itxn_field Amount
txn Sender
itxn_field Receiver
txn Sender
itxn_field CloseRemainderTo
itxn_submit

Thanks for the code. I think you can simplify it (though I haven’t tested it so test first if you want to use the simplified version):

itxn_begin
int axfer
itxn_field TypeEnum
txna Assets 0
itxn_field XferAsset
txn Sender
itxn_field AssetCloseTo
itxn_submit

itxn_begin
int pay
itxn_field TypeEnum
txn Sender
itxn_field CloseRemainderTo
itxn_submit
1 Like