Loop in n8n

Hi my dear friends.
In my case:
I want to add a row for special user to my spreadsheet in google sheets but at first I am forced to delete previous rows that record for that user.
My way is:

  • I search (lookout) my table.
  • I find all rows that record for that user (by user_id.
  • I try to delete that rows by row id.

My problem is that when I want to delete second row because of deleting previous row the new row id is not saved row id so my workflow delete wrong rows.

I think maybe the best way is search my table one by one and delete one by one. But how can I make loop in n8n.
(When I connect last node to search node it conflict with existing data).

Can anyone how can I delete some special existing rows in my Google spreadsheet by n8n?.

Best regards;

@mooghermez you can make a loop with the “split in batches” node. Check the example below.

   {
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n    {\n      json: {\n        articleId: 1,\n      }   \n    },\n        {\n      json: {\n        articleId: 2,\n      }   \n    },\n        {\n      json: {\n        articleId: 3,\n      }   \n    }\n]"
      },
      "name": "mockup_data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        120
      ]
    },
    {
      "parameters": {
        "batchSize": 1
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        670,
        120
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1360,
        150
      ]
    },
    {
      "parameters": {
        "operation": "delete",
        "sheetId": "asasasasas"
      },
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        900,
        120
      ],
      "credentials": {
        "googleApi": "asas"
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1110,
        370
      ]
    }
  ],
  "connections": {
    "mockup_data": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like