I have a function submission-created.js to delete Netlify Forms submissions as they come in, firing on the Netlify submission-created event.
Locally, using netlify dev and submitting a Form from localhost:8888, I can see the submissions are all deleted from the Netlify dashboard. This also works by manually calling it with netlify functions:invoke submission-created
Deployed and via a deploy preview URL, entries are not deleted when I submit a new form.
I really have no idea if it’s firing or not when deployed. I can only assume it isn’t because the form submissions are not deleted. It has never appeared in the Functions UI and the Netlify Support twitter account told me this is normal for lambda functions that are Netlify event hooks like mine (https://twitter.com/NetlifySupport/status/1303766897536581632) Really hard to debug this
The env vars are correctly set. I have one set custom and the other, SITE_ID, is a default
Deploy log slice so I know that it is being picked up during the build.
Update
Created a duplicate of the function with a non-event based name. Deployed it. Visited it in the browser e.g. /.netlify/functions/my-test-function. Seems to work.
Hey @dazulu,
I dug into this because I was like “that should totally work!” and in fact it does! I think there is something weird going on with with async/await and .forEach, maybe: JavaScript: async/await with forEach() | by Sebastien Chopin | codeburst? I’m not sure to be honest, but using an old school for loop instead worked for me:
# submission-created.js
const NetlifyAPI = require('netlify')
exports.handler = async function (event, context) {
const client = new NetlifyAPI(process.env.NETLIFY_PAT)
const submissions = await client.listSiteSubmissions({
site_id: "628de7ef-04fb-4530-a7da-b795c0e5a457" <--- no worries, this is not sensitive
})
.catch((e) => console.log('Error getting submissions', e))
if (submissions.length) {
for (i = 0; i < submissions.length; i++) {
console.log('starting to delete submission: ', submissions[i].id)
await client.deleteSubmission({ submission_id: submissions[i].id })
console.log('deleted submission: ', submissions[i].id)
}
} else {
console.log('NOTHING TO DELETE')
}
}
Thanks @jen Good article! A lesson in imperative vs declarative, perhaps
I was just about to reply with the post below (which I’m going to leave in case it helps anyone out), and it occurred to me that I never tested it deployed to production. I had only tested it working off the build preview URLs because I didn’t want the site live yet.
Long story short, I temporarily deployed it live and the function fires. It would seem event-based functions are not set up to fire from Preview Deploys
I’m sure I didn’t miss this footnote in the docs somewhere. Though I could be wrong. Please link me if anyone sees it. Otherwise this should probably be clarified there or, if it’s a bug, thrown in the backlog
Thanks again @jen - legend - I had given up on this and wouldn’t have tried again had you not replied!
Post below for historical purposes
The submission-created event function (only one I tested) only fires on a live site. It is not working on preview URLs.
I’ve adjusted the function but still something is not right when deployed
Test 1/2
Tested the refactored function locally with netlify dev and netlify functions:invoke submission-created:
Forms visible on the Netlify Admin dashboard are deleted
Submitting a form deployed on Netlify:
Forms visible on the Netlify Admin dashboard are not deleted
Build Log Extract
Just so we see the netlify dependency gets installed and the function is picked up.
Test 2/2
I duplicated the function (duplicate.js) so that I can call it manually when deployed (read that you can’t call deployed event-based functions manually - you get 403’d)
Called it in the browser - Get our console log from the function and forms visible on the Netlify Admin dashboard are deleted
Ayyyy, what a great post! Many thanks for the work you put into that If you wanted to start a topic explaining why you pursued this and sharing the article, please feel free- people may find it a bit more easily that way than by following this meandering thread