{
  "name": "Log Cleanup - 30 Day Retention",
  "nodes": [
    {
      "id": "schedule-trigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        250,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 2 * * *"
            }
          ]
        }
      },
      "notes": "Runs daily at 02:00. Configure timezone to Asia/Kolkata in n8n settings."
    },
    {
      "id": "calculate-cutoff-date",
      "name": "Calculate Cutoff Date",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        450,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "cutoff_date",
              "name": "cutoff_date",
              "value": "={{ $now.minus({days: 30}).toISO() }}",
              "type": "string"
            },
            {
              "id": "cutoff_formula",
              "name": "cutoff_formula",
              "value": "={{ \"IF(IS_BEFORE({timestamp}, '\" + $now.minus({days: 30}).toFormat(\"yyyy-MM-dd\") + \"'), TRUE())\" }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "search-old-records",
      "name": "Search Old Records",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        650,
        300
      ],
      "parameters": {
        "operation": "search",
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $env.AIRTABLE_BASE_ID }}"
        },
        "table": {
          "__rl": true,
          "mode": "name",
          "value": "AI_Logs"
        },
        "filterByFormula": "={{ $json.cutoff_formula }}",
        "returnAll": true,
        "options": {
          "fields": [
            "log_id"
          ]
        }
      },
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "loop-over-items",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        850,
        300
      ],
      "parameters": {
        "batchSize": 10,
        "options": {}
      }
    },
    {
      "id": "delete-records",
      "name": "Delete Records",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        1050,
        300
      ],
      "parameters": {
        "operation": "delete",
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $env.AIRTABLE_BASE_ID }}"
        },
        "table": {
          "__rl": true,
          "mode": "name",
          "value": "AI_Logs"
        },
        "id": "={{ $json.id }}"
      },
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "wait-rate-limit",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1050,
        450
      ],
      "parameters": {
        "amount": 250,
        "unit": "milliseconds"
      },
      "notes": "250ms delay between batches to stay under Airtable 5 req/sec rate limit"
    },
    {
      "id": "log-cleanup-summary",
      "name": "Log Cleanup Summary",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1250,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "cleanup_result",
              "name": "cleanup_result",
              "value": "Log cleanup completed",
              "type": "string"
            },
            {
              "id": "cleanup_date",
              "name": "cleanup_date",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            },
            {
              "id": "records_processed",
              "name": "records_processed",
              "value": "={{ $items().length }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Calculate Cutoff Date",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Cutoff Date": {
      "main": [
        [
          {
            "node": "Search Old Records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Old Records": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Delete Records",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Cleanup Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Records": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "CONFIGURE_ERROR_HANDLER_WORKFLOW_ID"
  },
  "tags": [
    {
      "name": "system"
    },
    {
      "name": "phase-1"
    },
    {
      "name": "scheduled"
    }
  ]
}