Delete All Attachments?

This discussion is public.

Timppa's Avatar

Timppa

02 Mar, 2025 02:07 PM

Is there a non-tedious way to delete all attachments from a MD dataset? I want to transition away from attachments and into keeping all my receipts in their own file structure. Step 1 is easy, I've extracted them all via Toolbox, but I cannot find an easy way to remove them from the dataset.

https://infinitekind.tenderapp.com/discussions/general-questions/13...

^^^ This thread implies you can delete them directly from the file system, then use Toolbox to fix the "Orphaned" attachments. When I try this, it finds the missing attachments, but doesn't show any orphaned ones to delete:

---------------------------------
Objects scanned: 27815
Transactions scanned: 27815
Transactions with attachments: 1725
Total Attachments referenced in Moneydance database (a txn may have multi-attachments): 1886
Attachments missing from Local Storage: 1886
Total Attachments found in file system: 0 (difference -1886)
Attachments on disk are taking: 0.0MB
 ---------------------------------
@@ ERROR: You have 1886 missing attachment(s) referenced on Moneydance Txns!

Following this, a pop-up, "YOU HAVE NO ORPHANS TO DELETE - NO ACTION TAKEN!"

I'm going to have to restore from backup, with the attachments, but would love a workflow that would permit me to accomplish this goal without tediously going through each transaction.

  1. 1 Stuart Beesley (Mr Toolbox)'s Avatar Stuart Beesley (Mr Toolbox) on 02 Mar, 2025 04:01 PM

    You can manually remove from the file system. BUT you have to disable sync first and delete all sync folders. I suggest you

    • backup
    • turn off sync
    • use toolbox to force reset sync settings
    • shut down MD
    • shut down all other datasets and mobile devices.
    • remove all sync folders
    • now delete the attachments folder
    • restart MD
    • toolbox. Fix orphans.
    • reset up sync from scratch.
  2. 2 Timppa's Avatar Timppa on 02 Mar, 2025 04:13 PM

    I've gone through all of those steps, including the sync disablement, reset settings, etc. The message is as I shared in my first post, no orphans found. All the transactions still have "attachments" on them, they're just pointing to files that no longer exist.

    Fix Orphans seems to only go after attachments not linked to transactions. If I run it before deleting the attachments directory it did find a handful (about a dozen) of attachments not linked to transactions. I made copies of the ones I cared about it and allowed it to delete them out of MD.

    It does not seem to fix attachments missing from the filesystem. It identifies them -- see the report above -- but it does not fix them. They remain with the transactions.

    This is on macOS 15.3.1, MD 2024.2, Toolbox 1068. I do see there's an update to 2024.4, so I'll install that and try the above process again....

  3. 3 Timppa's Avatar Timppa on 02 Mar, 2025 04:28 PM

    Same on 2024.4.

    Same of what it sees:

    ANALYSIS OF ATTACHMENTS
    
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/68f/8a5/46-/2023-07-09 11-18.pdf
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/e16/8f9/af-/2024-08-01 12-55.png
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/c37/71c/e0-/2023-09-12 18-14.pdf
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/d36/6e1/4b-/2024-04-07 10-48-46.pdf
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/f08/6cf/ef-/2024-11-23 13-16-58.pdf
    @@ Error - Attachment for Txn DOES NOT EXIST! - Attachment tag: attach/7b3/8fe/c6-/2024-03-29 10-49.pdf
    
    [snipped]
    
    @@ ERROR: You have 1886 missing attachment(s) referenced on Moneydance Txns!
    
    Attachment is missing from this Txn: AcctType: Schwab Checking Account: BANK Date: 04/23/2021 Value: -8295 AttachKey: [reg_attach_c5ea335f-5d21-4b35-8e66-312314d921c7]
    Attachment is missing from this Txn: AcctType: Verizon Visa Account: CREDIT_CARD Date: 04/23/2021 Value: -5154 AttachKey: [reg_attach_e98bd32e-6084-4168-9522-d49597d2770a]
    Attachment is missing from this Txn: AcctType: Verizon Visa Account: CREDIT_CARD Date: 04/26/2021 Value: -6736 AttachKey: [reg_attach_432190d9-7ffc-4f7e-b621-a2caca014771]
    
    [snipped]
    
    LISTING VALID ATTACHMENTS FOR REFERENCE
     ======================================
    
    Base Attachment Directory is: /Users/xxxx/Library/Containers/com.infinitekind.MoneydanceOSX/Data/Documents/xxxx/safe/
    -----------
    AT: CREDIT_CARD  ACT: Discover             DT: 05/03/2023 Val:     -200.0 FILE: attach/ab2/0d1/44-/2023-05-03 20-10.pdf
    
    [snipped]
    

    (Interestingly, none of those "valid attachments" exist either, I nuked the whole directory)

    tl;dr, it logs the errors, but it does not fix them. :(

  4. 4 Stuart Beesley (Mr Toolbox)'s Avatar Stuart Beesley (Mr Toolbox) on 02 Mar, 2025 04:43 PM

    You are correct... But do you care that txns reference attachments that no longer exist?

    I can probably give you a script to fix that if needed..?

  5. 5 Timppa's Avatar Timppa on 02 Mar, 2025 04:47 PM

    I would love such a script if it's not too terribly difficult.

    Most of the motivation here is to make my attachments accessible w/o MD, e.g., on my cell phone if I need one in a hurry for whatever reason. I've also noticed some performance degradation with my large attachment set, especially around syncing and backups, so that's a motivator too.

  6. 6 Timppa's Avatar Timppa on 02 Mar, 2025 05:59 PM

    I think I figured it out:

    #!/usr/bin/env python
    
    global moneydance
    mdGUI = moneydance.getUI()
    book = moneydance.getCurrentAccountBook()
    
    tSet = book.getTransactionSet()
    
    for txn in tSet:
        if (txn.hasAttachments()):
            for athkey in txn.getAttachmentKeys():
                print("removed attachment: %s" % athkey)
                txn.removeAttachmentTag(athkey)
    

    Not sure if this would work with sync enabled. I've seen various threads saying attachments can come back with sync enabled. I didn't manually delete the files in this case; the script took care of that. Can confirm they're all gone now, before and after:

    timppa@timppa-mbp safe % pwd
    /Users/timppa/Library/Containers/com.infinitekind.MoneydanceOSX/Data/Documents/timppa.moneydance/safe
    timppa@timppa-mbp safe % 
    timppa@timppa-mbp safe % du -s attach
    1022800 attach
    timppab@timppa-mbp safe % du -s attach
    0   attach
    timppab@timppa-mbp safe %
    

    Edited: Or not. The now invalid attachments come back after a MD restart. Strange. Back to drawing board...

  7. 7 Stuart Beesley (Mr Toolbox)'s Avatar Stuart Beesley (Mr Toolbox) on 02 Mar, 2025 06:21 PM

    Great!! Yes with sync enabled the files would reappear, but would be orphaned.

  8. 8 Timppa's Avatar Timppa on 02 Mar, 2025 06:45 PM

    To put it mildly, I don't fully understand the MD APIs and best practices. Syncing is disabled (via both opting out after restore from backup and Toolbox Reset Sync) yet the attachments return to the transactions after a restart.

    This script (one function added at the end, using one of the template scripts as a reference) solves the problem for good:

    #!/usr/bin/env python
    
    global moneydance
    mdGUI = moneydance.getUI()
    book = moneydance.getCurrentAccountBook()
    
    tSet = book.getTransactionSet()
    
    for txn in tSet:
        if (txn.hasAttachments()):
            for athkey in txn.getAttachmentKeys():
                print("removed attachment: %s" % athkey)
                txn.removeAttachmentTag(athkey)
                txn.getParentTxn().syncItem()
    

    Adding the syncitem() call prevents the invalid attachments from returning after a restart. I guess this is what commits changes to the file? It takes FOREVER, saving each item one-by-one, which makes me wonder if there's a better way to go about committing changes in a script like this? Is there a single call to commit changes?

    Happy my original issue is seemingly resolved but now I low key want to understand the API better so I can conjure up additional scripts to meet my needs. :D

  9. 9 Stuart Beesley (Mr Toolbox)'s Avatar Stuart Beesley (Mr Toolbox) on 02 Mar, 2025 07:03 PM

    Yup. syncItem() commits the change. It will only change the data record and not the underlying attachment file which will reappear with sync on. Well it will delete it, but sync will reload the file.

    Your script is fine with syncItem(). It just happens to be a slow api call x 1000s of txns.

  10. 10 Timppa's Avatar Timppa on 02 Mar, 2025 07:13 PM

    I definitely killed sync. On my list to set back up later today. I was just surprised it took so long to commit the change to each transaction. I suppose, thinking about it, there's a barely perceptible delay when making transaction changes via the GUI, so, like you said, X 1000s of transactions....

    Thanks for the help and guidance here. Hopefully this is useful to someone else in my shoes down the line.

    Edited to add: This definitely solved my annoyance with the delay for backups. My backup file is now created in under two seconds. Hopefully it helps with syncing. My annoyance previously was between the final sync + backup it could take upwards of 3 minutes just to close Moneydance.

  11. System closed this discussion on 01 Jun, 2025 07:20 PM.

Comments are currently closed for this discussion. You can start a new one.