Can I restore lost environment variables?

I have a Netlify site (http://tfsc-landing-page.netlify.app/) that was previously configured to deploy from a Bitbucket repository.

I have a new version of the site in a different repository, so I changed the build settings to point to the new repository, and all seemed well. However, I have just noticed that all the (20+) environment variables that I previously had configured have disappeared. Some of these were quite important and not trivial to recreate (access tokens and such).

Is there a way to restore the previous environment variables from before the build repository was changed?

Hiya @CamLAPC and sorry to be slow to get back to you!

Unfortunately, no - we don’t save those in any way (unless you created a new site, and the old site is still there, and you can still see the variables on it to copy and paste between).

Hey @fool thanks for getting back to me! I figured that might be the case – not to worry, I ended up recreating them by hand.

Might be good to have a warning on the ‘edit repository’ functionality that it will result in the loss of any existing environment configuration!

100% agree! I’ll bring it up with our documentation team since that would be nice to warn about as you start editing for sure.

Very much appreciate you taking time to make the suggestion!

Hmm. I just tried to reproduce this to inform the report to the docs team, and couldn’t - my variables remained in place after I edited my repo settings (linked to a different repo). Could you tell me in more detail the steps you took that resulted in the loss?

Yep, certainly:

  1. I went to the Netlify App admin for the site (in my case, https://app.netlify.com/sites/tfsc-landing-page/overview)
  2. I went to the Build & Deploy section
  3. I hit “Edit Settings”
  4. I hit “Link to a different repository”
  5. I went through the ‘connect to provider’/linking process (I’m using Bitbucket for both the old and new repositories)
  6. I configured the build settings (branch, build command, publish directory). Note that these did NOT seem to retain my previous settings, which seems reasonable given it is an entirely different repository. Also, both the old and new repositories have a netlify.toml that overrides the build command and publish directory anyway.
  7. I believe I clicked “New Variable” in the advanced build settings, but did not actually add any variables (because I thought they would come through from the previous configuration)
  8. I hit the deploy site button
  9. Then I had a bit of a panic since I had not seen the ‘old’ env variables listed, cancelled the deploy, and went over to the environment page and saw that they had in fact gone.

Hope that helps! I will try to reproduce on my end as well.

@fool Ok, I have been able to reproduce it, and it seems the specific thing that triggers the clearing of the environment is clicking the “New Variable” in the advanced build settings when changing the repository. As long as that option is NOT clicked, the environment is retained. However, if it is clicked, even if no actual variables are added, the previous environment variables are removed.

I guess this raises all kinds of interesting questions about how this should be handled, i.e. if new variables with the same name should automatically replace existing ones, but old ones should be merged in, and so on, which is probably why the existing behaviour is simply to remove the previous environment completely.

Regardless, it seems counter intuitive to me to have the existing variables either be invisibly kept or lost depending on whether you enter some new ones during the repository change, or even just hit the button to add new ones without actually doing so.

1 Like

Wow, thank you for the very thorough report! I’ll get a bug filed; I don’t think that is intended (I think they should always be saved, even if you add a new one!)

Not a problem, happy to help!

1 Like