How to see which file resources have no associated records?

Options

It turns out I was skipping the "delete file resource" step, so I've apparently got a bunch of files in my file management that are no longer associated with any records.

How can I isolate those to clean things up?

Answers

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    Diffing lists is hard! I would do the following in a function:

    1. Create an empty array "to_delete"

    2. Query all records to pull the filenames of the documents you have registered in whatever table.
    3.Repeat 1 across additional tables that reference files
    4. merge the arrays you developed in 2 and 3 to get your list of "good files"
    5. Use the metadata api to get lists of files in your system.
    6. iterate (for…each) over those lists.

    7. create conditional (if…then) asking if the array from "3" "has any element" of the current filename.
    8. If not, "append to end" the current item to "to_delete"

    Then run your function to ask if "to_delete" looks right. debug as you need.

    After confirming the list looks good, you can add another loop to your code:

    1. for each in to_delete
    2. delete the file

    Let me know if the above helps?

    This kind of tricky data problem and devops pain is very much in the tricky category that is part of the hardest 5% we work on at State Change office hours and forum literally every day.

  • Erin
    Erin Member
    Options

    Ah, the metadata api! Haven't used that yet and didn't realize it would have that info. Thanks @Ray Deck !