/n8n-tutorials

How to restore deleted workflows in n8n?

Learn how to restore deleted workflows in n8n by updating the 'deletedAt' field in the database or using the n8n REST API. Step-by-step guide included.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to restore deleted workflows in n8n?

To restore deleted workflows in n8n, you need to access the database where n8n stores workflow data, locate the deleted workflows in the workflow_entity table, and update their 'deletedAt' field to NULL. This process requires database access and SQL knowledge, and can be done through direct database manipulation or by using n8n's REST API depending on your setup.

 

Detailed Guide to Restore Deleted Workflows in n8n

 

Step 1: Understanding How n8n Handles Deleted Workflows

 

When you delete a workflow in n8n, it's not immediately purged from the database. Instead, n8n uses a "soft delete" approach where it sets a 'deletedAt' timestamp in the database record. This means that deleted workflows still exist in the database and can potentially be recovered.

 

Step 2: Prerequisites

 

Before you begin the restoration process, you'll need:

  • Access to the database where n8n stores its data
  • Basic knowledge of SQL
  • Database management tools appropriate for your database type (e.g., pgAdmin for PostgreSQL, MySQL Workbench for MySQL)
  • A backup of your database (recommended before making any changes)

 

Step 3: Identify Your Database Type

 

n8n supports several database types, including:

  • SQLite (default for development and small deployments)
  • PostgreSQL
  • MySQL/MariaDB
  • Microsoft SQL Server

Your approach may vary slightly depending on which database you're using.

 

Step 4: Connect to Your Database

 

For SQLite:

The database file is typically located in the n8n data directory. Use a tool like SQLite Browser to open the file.

For PostgreSQL:

Connect using pgAdmin or the psql command-line tool:


psql -h hostname -U username -d n8n\_database

For MySQL/MariaDB:

Connect using MySQL Workbench or the mysql command-line tool:


mysql -h hostname -u username -p n8n\_database

For MS SQL Server:

Use SQL Server Management Studio or the sqlcmd utility to connect.

 

Step 5: Locate Deleted Workflows

 

Once connected to your database, execute a query to find all deleted workflows:


SELECT id, name, active, deletedAt FROM workflow\_entity WHERE deletedAt IS NOT NULL;

This query will show you all workflows that have been deleted, along with their IDs, names, and when they were deleted.

 

Step 6: Restore a Specific Workflow

 

To restore a specific workflow, you'll need to set its 'deletedAt' field to NULL. Use the following SQL query, replacing [WORKFLOW_ID] with the actual ID of the workflow you want to restore:


UPDATE workflow_entity SET deletedAt = NULL WHERE id = [WORKFLOW_ID];

For example, to restore a workflow with ID 42:


UPDATE workflow\_entity SET deletedAt = NULL WHERE id = 42;

 

Step 7: Restore Multiple Workflows at Once

 

If you want to restore all deleted workflows:


UPDATE workflow\_entity SET deletedAt = NULL WHERE deletedAt IS NOT NULL;

To restore workflows deleted within a specific timeframe:


UPDATE workflow\_entity SET deletedAt = NULL 
WHERE deletedAt BETWEEN '2023-01-01' AND '2023-01-31';

 

Step 8: Verify the Restoration

 

After executing the update query, verify that the workflows have been restored by running:


SELECT id, name, active FROM workflow_entity WHERE id IN ([WORKFLOW_ID1], [WORKFLOW\_ID2]);

Replace [WORKFLOW_ID1], [WORKFLOW_ID2], etc. with the IDs of the workflows you restored.

 

Step 9: Restart n8n (If Necessary)

 

In some cases, you might need to restart your n8n instance for the changes to take effect:


# If using pm2
pm2 restart n8n

# If using systemd
sudo systemctl restart n8n

# If running with Docker
docker restart n8n-container

 

Step 10: Alternative Method - Using n8n REST API

 

If you have admin access to n8n but not direct database access, you can use the n8n REST API to restore workflows. This method requires you to have an API key.

First, authenticate with the API:


curl -X POST \\
  http://your-n8n-instance/rest/login \\
  -H 'Content-Type: application/json' \\
  -d '{
    "email": "[email protected]",
    "password": "your-password"
  }'

This will return a session cookie that you'll use for subsequent requests.

Then, get a list of deleted workflows:


curl -X GET \\
  http://your-n8n-instance/rest/workflows?includeDeleted=true \\
  -H 'Cookie: sessionid=your-session-cookie'

Finally, restore a specific workflow:


curl -X PUT \\
  http://your-n8n-instance/rest/workflows/[WORKFLOW\_ID]/restore \\
  -H 'Cookie: sessionid=your-session-cookie'

Replace [WORKFLOW_ID] with the ID of the workflow you want to restore.

 

Step 11: Troubleshooting Common Issues

 

Issue: Workflow doesn't appear in n8n after restoration

  • Ensure n8n has been restarted after the database changes
  • Check if the workflow has the 'active' flag set to true
  • Verify that all workflow-related records were properly restored

Issue: Database errors during restoration

  • Make sure you have the correct permissions to modify the database
  • Check for any constraints that might be preventing the update
  • Verify that you're connected to the correct database

Issue: API method returns errors

  • Ensure your authentication token/cookie is valid and not expired
  • Verify that you have admin permissions in n8n
  • Check if the workflow ID is correct

 

Step 12: Preventive Measures for the Future

 

To avoid needing to restore workflows in the future:

  • Regularly export your workflows as JSON from the n8n interface
  • Set up regular database backups
  • Consider using version control for your workflow exports
  • Implement a workflow management process to prevent accidental deletions

 

Step 13: Understanding Database Schema Changes

 

Be aware that the database schema might change between n8n versions. Always check your specific version's database structure if the standard commands don't work. You can examine the schema with:


-- For PostgreSQL or MS SQL
SELECT column_name, data_type 
FROM information\_schema.columns 
WHERE table_name = 'workflow_entity';

-- For MySQL/MariaDB
DESCRIBE workflow\_entity;

-- For SQLite
PRAGMA table_info(workflow_entity);

 

Step 14: Special Considerations for Self-Hosted Environments

 

If you're using a self-hosted n8n instance, you might have additional options:

  • Access to filesystem backups that might contain previous database states
  • Direct access to configuration files that might affect how deletions are handled
  • Ability to modify retention policies for deleted workflows

In some self-hosted configurations, you might be able to restore from a database backup instead of manipulating the current database directly.

 

Conclusion

 

Restoring deleted workflows in n8n is possible through direct database manipulation or API calls. The most straightforward method is to update the 'deletedAt' field in the workflow_entity table to NULL for the workflows you want to restore. Always back up your database before making changes, and verify that the restoration was successful by checking the n8n interface after restarting the service if necessary.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022