Automated Execution Log Cleanup for Instance Optimization - n8n Workflow

Use this powerful n8n workflow template to automatically clean up old execution logs in your n8n instance. Improve performance, reduce storage, and keep only the recent executions using the official n8n API.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Markdown-formatted string:

n8n Instance Administrators: Users managing self-hosted or n8n cloud instances needing to optimize database performance.
DevOps Teams: Professionals looking for automated maintenance solutions using an n8n workflow.
Users with High Execution Volume: Individuals whose n8n workflow runs hundreds of times per day, leading to storage bloat.
n8n Developers: Those seeking a robust example of utilizing the internal n8n API node for system management tasks.

Overview

Managing the execution logs generated by active n8n workflows is crucial for maintaining performance and minimizing storage costs. If you run many n8n templates frequently, your database can quickly become bloated, slowing down the n8n UI. This innovative n8n workflow solves this problem by providing a hands-off, scheduled cleanup mechanism. It uses the internal n8n API to retrieve execution data, applies a retention rule (keeping the N most recent runs per workflow), and uses the n8n node dedicated to execution deletion. This ensures that only relevant, recent data remains, drastically improving your overall n8n experience. This entire process relies solely on core n8n nodes, making it reliable across both n8n Cloud and self-hosted environments.

How it Works

Markdown-formatted string:


  1. Scheduled Start: The n8n workflow is initiated daily at 2 AM using the Schedule Trigger n8n trigger.

  2. Fetch Executions: The n8n node 'Get many executions' calls the internal n8n API to fetch up to 250 of the most recent execution records across all active and inactive workflows.

  3. Define Retention Policy: The 'Set Executions to Keep' n8n node introduces the executionsToKeep variable, which is set to 10 by default, defining how many recent executions must be retained per unique workflow ID.

  4. Filtering Logic: The Code n8n node executes custom JavaScript. This powerful n8n node performs three key tasks: grouping all fetched executions by their workflowId, sorting each group by start time (newest first), and identifying all executions that exceed the configured executionsToKeep limit. The output is a filtered list containing only the execution items scheduled for deletion.

  5. Execution Deletion: Finally, the 'Delete an execution' n8n node iterates through every item passed by the Code node and uses the n8n API to permanently delete the outdated execution record. This completes the cleanup cycle of the n8n workflow.

Installation Guide

Markdown-formatted string:


  1. Import the n8n workflow: Copy the provided JSON data and paste it directly into your n8n instance via the 'New' button and 'Import from JSON'.

  2. Create an n8n API Key: Navigate to Settings > API Keys in your n8n instance and create a new Personal API Key.

  3. Set Up n8n API Credentials: Create a new n8n API credential.

Set the API Key to the key created in step
2.
Set the Base URL to your n8n instance URL appended with /api/v1 (e.g., https://my.n8n.cloud/api/v1).

  1. Configure API Nodes: Apply this newly created n8n API credential to both the 'Get many executions' n8n node and the 'Delete an execution' n8n node.

  2. Customize Retention: Open the 'Set Executions to Keep' n8n node. Adjust the executionsToKeep value if you wish to retain a different number of recent logs (e.g., set it to 5 instead of 10).

  3. Activate: Save and activate the n8n workflow to begin the automated cleanup process based on the Schedule Trigger n8n trigger.

Node Details

Markdown-formatted string:

Schedule Trigger (n8n trigger)
Function: Starts the n8n workflow automatically.
Key Configuration: Set to run daily at 2 AM (triggerAtHour: 2).
Get many executions (n8n node - n8n API)
Function: Queries the n8n API for a list of recent execution logs.
Key Configuration: Resource: execution, Limit: 250 (max limit per call). Requires n8n API credential.
Set Executions to Keep (Set n8n node)
Function: Defines the retention rule that the workflow must follow.
Key Configuration: Creates the variable executionsToKeep with a value of 10 (the number of most recent executions to keep per workflow).
Code (n8n node)
Function: Executes the complex logic to group executions by workflow ID, sort them by date, and filter out those that are older than the retention limit.
Key Configuration: Custom JavaScript logic that identifies IDs of executions eligible for deletion.
Delete an execution (n8n node - n8n API)
Function: Calls the n8n API to delete specific execution records.
* Key Configuration: Operation: delete, Execution ID: dynamically set to ={{ $json.id }} from the previous step. Uses the same n8n API credential.

Related n8n Workflows

Free

Nodes: 5 Nodes
Updated: December 26 2025
View all
Created by

👋 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

Featured*