Hi, I’m new to n8n and need some help with a workflow I’m trying to implement. I want to prevent sending content to leads more than once if they have been stored in the database for less than 30 days. The leads will initially be sent from Clay, passed through a Webhook, and then stored in a pSQL table.
Here’s what I want the workflow to do:
Check the number of days a lead has been in the database
If a lead has been in the database (Smartlead/Instantly) for more than 30 days, delete it
Could anyone provide guidance on how to accomplish this? Any help would be greatly appreciated. Thank you!
What is the error message (if any)?
Please share your workflow
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)
Perhaps you can confirm where exactly you’re having trouble here? Based on your description it seems you’re mostly wondering how to work with the database using n8n, is this correct?
Check the number of days a lead has been in the database
This should be doable by running a query such as this (where created_at is the creation date of your lead and leads is your table name):
SELECT
id,
EXTRACT(
DAY FROM NOW() - created_at
) AS lead_age
FROM
leads;
This will return the id field of your leads along with their age, but you can of course also query other or additional fields you need (like the email of a lead):
If a lead has been in the database (Smartlead/Instantly) for more than 30 days, delete it
In n8n this requires two more nodes:
An If node to check if a lead is older than 30 days (or not)
Another PostgreSQL node handling the actual deletion
In a workflow this could like so:
Hope this helps! Keep in mind that I’ve created this using my own test data, your data structure is likely different and you’d need to adjust the SQL queries accordingly.