If you move workflows from production to staging with Git source control, the CLI or copy and paste, a copied workflow keeps its Schedule or polling trigger. Staging then runs your production jobs on its own: it spends executions and calls real APIs.
This workflow runs nightly on the staging instance and catches that. It lists every published workflow through the n8n API, keeps the ones with a Schedule, Cron, Interval or polling trigger that don’t carry an allow-list tag (default run-in-staging), and skips itself.
Dry run is on by default, so the first runs only post a Slack report of what it would unpublish. Set dryRun to false and it unpublishes them, then posts the same report.
Setup takes six steps: an API key on staging, an n8n API credential in both n8n nodes, the Set configuration node (tag, instance label, dry run), tags on the workflows that should keep running, a Slack channel (or swap Slack for email), and one manual run to read the report before you publish it.
You can also trigger it right after your Git pull instead of at night, or set includePollingTriggers to false to touch schedules only.
Template: Unpublish staged timed workflows via the n8n API and report to Slack | n8n workflow template
2 Likes
Nice one, and dry run on by default is the right call for something that unpublishes workflows.
One thing that pairs well with it: point the staging credentials at sandbox or test accounts. Source control only syncs credential stubs, not the secrets, so you fill them in per instance anyway. Then if a schedule slips past the check, it hits test APIs instead of real ones.
I’d also keep the nightly report even on nights when nothing needs unpublishing (“0 timed workflows found”). Otherwise a quiet report and a check that stopped running look the same.
One gap worth closing in the check itself: listing every published workflow is only true for the first page — the public API caps a workflow-list response and hands back a cursor, so on any instance with more workflows than fit one page the nightly sweep quietly skips the rest while still reporting a clean run.
The other thing a sweep can’t cover is the window between sweeps: a workflow published in the morning fires against production before the check runs that night. A cheap runtime guard closes it — make the first node after the trigger an IF on an env value or a config row (proceed only when it says staging), so a slipped schedule fails immediately instead of calling the real API, with the nightly check left as the cleanup.
And after deactivating, re-read the workflow’s active flag and write id, name, trigger type and time somewhere durable. The API returning success is not the same as the scheduler having stopped, and re-activating after a bad allow-list edit is manual work the Slack message won’t help you reconstruct.
Useful template, and dry run by default is the right call. Some measurements that may sharpen it, from testing staging copies on n8n 2.44.6 and the 3.0 nightly:
- `n8n import:workflow` brings every workflow in unpublished, including ones that were published where they were exported. So export/import alone doesn’t carry live schedules over. The routes that do are the ones that copy the database.
- If staging is a restored copy of the production database, `n8n unpublish:workflow --all` before the first start stops the triggers. It does not stop executions that were waiting (Wait nodes, forms): those resume after the restart and finish for real.
- A restored database won’t start without production’s encryption key, so a restored staging copy always holds working production credentials. That makes @TaskToAgent’s point about pointing staging at test accounts the real fix, not a nice-to-have.
Disclosure: I build Preflight, a replay-testing tool for n8n. The longer write-up, aimed at testing the 3.0 upgrade on a copy safely: Test Your Workflows Before Upgrading to n8n 3.0 | Preflight