What Happens to a Preview Environment When a Pull Request Merges or Closes?
GuideSeptember 25, 20263 min read

What Happens to a Preview Environment When a Pull Request Merges or Closes?

Short answer: With Bunnyshell, a pull request's ephemeral environment is destroyed automatically the moment the PR merges or closes — no manual cleanup, no follow-up ticket, no forgotten environment quietly running (and billing) for weeks afterward.

How It Works

This is controlled by a single setting on the primary environment that spawns ephemeral copies:

Bash
bns environments update-settings --id <PRIMARY_ENV_ID> \
  --destroy-ephemeral-on-pr-close

Once enabled, Bunnyshell's Git provider integration listens for the merge/close webhook and triggers a real delete pipeline — the same operation you'd run manually:

Bash
1$ bns environments delete --id 5bDn8MrEgm
2
3Environment 5bDn8MrEgm scheduled to delete with EventID V2GY16VBGR
4EventID DVbvWRj3Jd generated delete pipeline GqWe1wDRW8
5Environment 5bDn8MrEgm successfully deleted

Deletion removes the namespace and everything in it — compute, storage, ingress, the routed hostname — so nothing keeps running (or billing) after the PR's job is done.

What If You Want to Keep It a Bit Longer?

Two options: disable the auto-destroy setting for that one case via the CLI or UI before merging, or set termination protection on the specific environment (bns environments create ... --termination-protection), which exempts it from automatic teardown while leaving the setting on for everything else.

For the full mechanics — including how to add a stale-environment safety net for PRs that never formally close — see Auto-Destroy After Merge.

Frequently Asked Questions

Does the environment get destroyed immediately on merge?

Yes, the moment the merge or close webhook is received, a delete pipeline is triggered automatically — no manual step or delay required.

Is there a way to keep a merged PR's environment running a bit longer?

Yes, either by disabling destroyEphemeralOnPrClose for that specific case or by setting termination protection on the environment before merge, which exempts it from automatic teardown.

What if the PR is closed without merging?

The same teardown fires. Both merge and close-without-merge are treated as terminal PR states that end the environment's reason for existing.