tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/5273-proper-way-to-delete-a-transactionInfinite Kind: Discussion 2021-06-29T15:50:27Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-29T22:30:55Z2021-03-29T22:30:55Zproper way to delete a transaction?<div><p>Hi Jim,</p>
<p>Great questions! To completely delete a transaction please get the parent and call deleteItem() on that. All transactions have a .getParentTxn() method so you can reliably call .getParentTxn().deleteItem().</p>
<p>However in some cases you might not want to delete every part of a transaction, such as when you have a SplitTxn that is part of a multi-split transaction (such as most transactions in investment accounts). In cases like those you'd want to check if you have a split that is part of a multi-split transaction. So maybe something like the following:</p>
<pre>
<code>ptxn = txn.getParentTxn()
if ptxn == txn: # txn is the parent, so delete it
ptxn.deleteItem()
else if ptxn.getOtherTxnCount()>1: # txn is the only split, so delete the parent
ptxn.deleteItem()
else: # txn is one of multiple splits
txn.deleteItem() # this will remove the split from the parent
ptxn.syncItem() # but we still need to save the modified parent</code>
</pre>
<p>Thanks,<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-30T02:05:10Z2021-03-30T02:05:10Zproper way to delete a transaction?<div><p>Thanks Sean!</p>
<p>Your response of course triggers a thought I want to confirm. I'm<br>
getting the transaction from a set like this:</p>
<p>txns =<br>
moneydance_data.getTransactionSet().getTransactionsForAccount(BankAccountName)</p>
<p>My understanding is that because I'm using the name of a bank account<br>
and not a 'category', then the txn set would only include parent<br>
transactions. Is that a correct assumption? I'm currently skipping this<br>
step: ptxn = txn.getParentTxn() , but wondering if I'm at any risk of<br>
buggering things up.</p>
<p>(In my case I'm not looking to preserve any splits so it seems like just deleting the parent will also delete the splits.)</p>
<p>I was also wondering ..if a ParentTxn has multiple splits and you<br>
ParentTxn.removeSplit() on one of them (and don't delete it), what<br>
happens to that split? Will it delete itself or will it get orphaned and<br>
hang around somehow/where?</p>
<p>Thanks,</p>
<p>Jim</p></div>J Ctag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-30T04:20:01Z2021-03-30T16:17:43Zproper way to delete a transaction?<div><p>You will (may) get both parents and splits in your txn set even when you select an account. This is because txfrs between accounts can be initiated from either account / ‘side’. Thus if you get a split it may be the other half of an account txfr or even the Xfr part of a investment BuyXfr or sell Xfr.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-30T05:22:39Z2021-03-30T15:53:38Zproper way to delete a transaction?<div><p>Just checked... Yup if you use ParentTxn.removeSplit() then you should call ParentTxn.syncItem() afterwards; that's it. But take care if you are deleting the last split - as Sean mentions above. Also take care; ideally use ParentTxn.setEditingMode() if you are going to make multiple changes to a parent before you call .syncItem().</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-30T15:07:02Z2021-03-30T15:07:04Zproper way to delete a transaction?<div><p>Thanks for the clarification Stuart!</p>
<p>Jim</p></div>Jimtag:infinitekind.tenderapp.com,2009-01-14:Comment/491145242021-03-30T15:44:51Z2021-03-30T15:44:51Zproper way to delete a transaction?<div><p>Hi Jim,</p>
<p>Stuart's answer to the first question is correct.</p>
<p>For the second question, a deleted split should only stick around if you keep a reference to it... so as long as you (and other extensions) don't hold onto it, it will be garbage collected by java and never seen again.</p>
<p>Cheers,<br>
Sean</p></div>Sean Reilly