8
n8n 中文网amn8n.com

自动化执行清理系统

中级

这是一个DevOps领域的自动化工作流,包含 12 个节点。主要使用 N8n, Set, Code, ScheduleTrigger 等节点。 使用n8n API和自定义保留规则的自动化执行清理系统

前置要求
  • 无特殊前置要求,导入即可使用
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "0a82a9d5784b67ad963ceff89283b2e40cb771be10fe58ba2682f3598a37c6e1",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "fc2eb1d2-8c2e-4956-a084-ef685652cdbd",
      "name": "定时触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        320,
        -32
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 2
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f528c67c-0077-4d84-a756-5b9020b838d8",
      "name": "获取多个执行记录",
      "type": "n8n-nodes-base.n8n",
      "position": [
        816,
        -32
      ],
      "parameters": {
        "limit": 250,
        "filters": {},
        "options": {
          "activeWorkflows": false
        },
        "resource": "execution",
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "VSxAuLMW3kDlXjYA",
          "name": "EMPTY"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9e3f98f2-f2f6-4d15-8858-1dacc46582a0",
      "name": "代码",
      "type": "n8n-nodes-base.code",
      "position": [
        1824,
        -32
      ],
      "parameters": {
        "jsCode": "const executionsToKeep = $input.first().json.executionsToKeep ?? 10;\n\n// Filtrar ejecuciones válidas (con workflowId y startedAt)\nconst validExecutions = items.filter(item =>\n  item.json.workflowId && item.json.startedAt\n);\n\n// Agrupar por workflowId\nconst grouped = {};\nfor (const item of validExecutions) {\n  const wfId = item.json.workflowId;\n  if (!grouped[wfId]) grouped[wfId] = [];\n  grouped[wfId].push(item);\n}\n\n// Ordenar por startedAt descendente (más reciente primero)\nfor (const wfId in grouped) {\n  grouped[wfId].sort((a, b) =>\n    new Date(b.json.startedAt) - new Date(a.json.startedAt)\n  );\n}\n\n// Obtener ejecuciones a eliminar\nlet toDelete = [];\nfor (const wfId in grouped) {\n  const executions = grouped[wfId];\n  if (executions.length > executionsToKeep) {\n    toDelete = toDelete.concat(executions.slice(executionsToKeep));\n  }\n}\n\nreturn toDelete;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "68e8939a-10f0-4a65-a56c-5f643d2dceec",
      "name": "删除执行记录",
      "type": "n8n-nodes-base.n8n",
      "position": [
        2304,
        -32
      ],
      "parameters": {
        "resource": "execution",
        "operation": "delete",
        "executionId": "={{ $json.id }}",
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "VSxAuLMW3kDlXjYA",
          "name": "EMPTY"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c59692b1-8dc8-44f8-bd31-dcc3898a2978",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -800,
        -880
      ],
      "parameters": {
        "width": 896,
        "height": 1680,
        "content": "# 🧹 在n8n中自动清理旧执行记录(每个工作流仅保留最新记录)"
      },
      "typeVersion": 1
    },
    {
      "id": "8ecf0fc5-9917-474c-b3f2-32d45ecf7059",
      "name": "设置要保留的执行记录",
      "type": "n8n-nodes-base.set",
      "position": [
        1328,
        -32
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a7f3a2de-0606-434c-8a81-4bf64e766899",
              "name": "executionsToKeep",
              "type": "number",
              "value": 10
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "a249ad6e-6abc-42b8-a272-506c0f3f76a6",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        -720
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 1520,
        "content": "# 🕒 计划触发器"
      },
      "typeVersion": 1
    },
    {
      "id": "7f7c076d-2bb6-4cef-88c2-6fef8e22f4e1",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        624,
        -720
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 1520,
        "content": "# 📥 获取多个执行记录"
      },
      "typeVersion": 1
    },
    {
      "id": "b2be8559-6cbd-4778-8830-4fa467efe363",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1120,
        -720
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 1520,
        "content": "# 🛠️ 设置要保留的执行记录"
      },
      "typeVersion": 1
    },
    {
      "id": "f31f51a1-70ba-46f5-adbb-4752b6c6d2c8",
      "name": "便签说明4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1616,
        -720
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 1520,
        "content": "# 🧠 代码节点(过滤要删除的执行记录)"
      },
      "typeVersion": 1
    },
    {
      "id": "a7b5add2-eab2-4b79-b7d3-6e84fb4dc118",
      "name": "便签说明5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2112,
        -720
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 1520,
        "content": "# 🗑️ 删除多个执行记录"
      },
      "typeVersion": 1
    },
    {
      "id": "00e57fb6-961a-4e85-81c6-2de04f301a93",
      "name": "便签 6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        -880
      ],
      "parameters": {
        "color": 3,
        "width": 2432,
        "height": 128,
        "content": "# ❗ 此工作流会删除数据。在生产环境启用前,请务必在暂存环境中测试。"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Delete an execution",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get many executions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete an execution": {
      "main": [
        []
      ]
    },
    "Get many executions": {
      "main": [
        [
          {
            "node": "Set Executions to Keep",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Executions to Keep": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 开发运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量12
分类1
节点类型5
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Arlin Perez

Arlin Perez

@arlindeveloper

👋 Hi! I'm Arlin - a QA Engineer with 2 years of experience and a background in Dart & Flutter for mobile app development. ⚙️ I'm passionate about automation and love building efficient workflows using n8n. 📬 Reach out for any help with custom workflows

外部链接
在 n8n.io 查看

分享此工作流