Google Drive Move within Loop Failing on 2nd Move (1st Works) - GAH!

Describe the problem/error/question

I have a workflow that works perfectly to look in a folder for invoices, OCRs them with Mistral, then emails the processed output to an email. It loops through all the files correctly, and completes when done correctly. Yay.

The problem I’m having is when I add a “Google Drive / Move Files” node to move processed files from an “Invoices” to an “Invoices-Processed” folder.

When I add that node, the 1st loop works correctly and moves file to the folder. Then the loop restarts and works correctly all the way until the second time the Move File node execute. On the second loop on that node, I get the following error:

What is the error message (if any)?

The resource you are requesting could not be found

Request failed with status code 404

Information on your n8n setup

  • n8n version: Community (latest)
  • Database (default: SQLite): SQLLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: ubuntu 24

Here’s the relevant parts of the workflow

Here’s the output of the Edit Fields1 node I’m using to debug, the first time it executes (correctly). You can see that the different ways to call the relevant file ID all work correctly:

Here’s the output of the Edit Fields1 node on second run, when it fails:

It feels like there’s context being lost in the loops, and I can’t find the right way to reference the file id being worked on within the loop. Thanks in advance!

Hey @defconjuan ,

It would be difficult to debug this with you .

Can you check on each node what’s happening and where exactly the data is being lost ?

Compare the working output to the non working one on each stage

1 Like

Thanks for the quick reply!

I did some additional work and believe the context is being lost during the second pass through the fileTypeSwitch switch node. Here’s what I did:

Working:

  • Disconnected all of the steps after the start of the sendFile loop
  • Added a set fields node for debugging (editFields2) and made it the first step after the sendFile loop. This sets thisFileID = listFiles.json.item.id.
  • Connected editFields2 node exit back to the sendFile loop.
  • The result: The file id from listFiles.item.json.id and that was set as thisFileID during the editFields2, were accurate, not null, and changed on each loop. Yay.

Also working:

  • Then I brought-in the Google Drive sendFile node and reconnected it to the sendFile loop, and it worked as expected – i.e. I was able to get the file id several ways and on each loop, not just the first. Yay again.

Not working:

Here’s where we lose context. When I connect the fileTypeSwitch node to the workflow, we go back to only the 1st loop working AND the 2nd loop failing because it cannot find the file ID even though it’s called the same way between the 1st and 2nd loop.

No matter how I call for the id (ex: json.id, $('SendFile').item.json.id, etc.) after the switch node, it come’s back null on the 2nd+ loop.

Questions:

  • Is using a switch within a loop causing context to be lost after the 1st loop?
  • Is there another way to access data from outside a loop (i.e. is there a way to specifically access listFiles data and use the loop’s run index to forcibly call a specific data structure outside of the loop?

Here are my fileTypeSwitch configs if that helps:


After figuring out that the switch node seems to be where context is being lost on the second loop, here’s a working workflow BUT it moves the file BEFORE the confirmation email goes out, and I only want to move the file after tat, so that we can not move files that fail to OCR or be processed by the Agent. So it’s not right, yet… Appreciate all help.