---
title: Manage environments
description: Create environments, copy code and data into them, point a folder at one, and delete them when you are done.
related:
  - /concepts/organisations-projects-and-environments
  - /develop/develop-with-luna-dev
  - /develop/deploy-from-ci
---

Trying a change on the lunas your app's users rely on puts their data at risk. An environment is a separate copy of your project, with its own lunas, data, keys and secrets, where a change can fail safely.

Every project starts with `production`. [Organisations, projects and environments](/concepts/organisations-projects-and-environments) explains how they fit together.

## Create an environment

[`luna env create`](/cli/env) makes an empty environment in the folder's project, and the folder then uses it.

```bash
luna env create dev
```

The command writes `LUNA_PROJECT` and `LUNA_ENV` to `.env.local`, and later commands in the folder act on that environment. Once the environment has code, `.env.local` also gets its `LUNA_URL` and `LUNA_KEY`.

A name is lower-case letters, digits and single dashes, up to 39 characters. It starts and ends with a letter or a digit, and `production` is taken.

## Copy code from another environment

Pass `--from` to start with another environment's code and settings, such as its limits and labels.

```bash
luna env create staging --from production
```

The new environment gets its own URL and keys. It does not take the source's extra host names, and it never takes its secrets. Set those with [`luna secrets set`](/cli/secrets).

The source must have something deployed. `--from` an environment that was never deployed to is refused.

## Copy data too

Add `--data` to copy the source's lunas as they are now, on top of its code.

```bash
luna env create staging --from production --data
```

Luna does the copying, and the command prints how many lunas it has copied so far. Stopping the command does not stop the copy. Pass `--no-wait` to return at once while the copy goes on.

Copying data reads all of the source, so it needs the `data:read` permission on that environment.

## Delete an environment on a timer

Pass `--expires-in` to have Luna delete the environment by itself later. Write the time as `90m`, `12h`, `7d` or a number of seconds.

```bash
luna env create try-search --from production --expires-in 7d
```

## Point a folder at an environment

`luna env use` switches the folder to an environment that exists.

```bash
luna env use staging
```

It writes the environment's URL and a publishable key to `.env.local` as `LUNA_URL` and `LUNA_KEY`. When the folder's `package.json` uses a front-end framework, it also writes copies under the names that framework passes to the browser.

| Framework | Names |
| --- | --- |
| Next.js | `NEXT_PUBLIC_LUNA_URL`, `NEXT_PUBLIC_LUNA_KEY` |
| Vite | `VITE_LUNA_URL`, `VITE_LUNA_KEY` |
| Expo | `EXPO_PUBLIC_LUNA_URL`, `EXPO_PUBLIC_LUNA_KEY` |

Any other command can act on another environment once with `--env`, such as `luna logs --env production`.

## List environments

```bash
luna env list
```

The list shows each environment's version, URL and tenants, and marks the folder's own with `*`. An environment appears as soon as it is made, before anything is deployed to it.

## Delete an environment

[`luna env delete`](/cli/env) removes an environment with all its lunas, data, keys and secrets. It asks first, and `--yes` answers in advance.

```bash
luna env delete try-search
```

:::warning
A deleted environment cannot be brought back, and `luna restore` cannot reach it. Export what you want to keep first with `luna export`.
:::

Production cannot be deleted this way. It goes when its project is deleted. If the folder used the environment you deleted, its `.env.local` no longer names an environment.

## One environment for each developer

Give each person their own environment, so that `luna dev` deploys and logs never mix.

```bash
luna env create dev-ann --from production
```

Each developer runs this once in their own clone. `.env.local` is their own file, so the folder points at their environment and nobody else's.

## One environment for each pull request

A pipeline can make a short-lived environment for each pull request, deploy to it, and let it expire.

```bash
luna env create pr-42 --from production --expires-in 3d
```

```bash
luna deploy --env pr-42 --yes
```

A deploy refuses an environment that does not exist, so create it first. Without `--from`, it starts with no code, data or secrets.

## Next steps

- [Deploy from CI](/develop/deploy-from-ci) runs these commands from a pipeline with an API token.
- [Store secrets](/develop/store-secrets) sets the secrets a new environment starts without.
- [Fork a luna](/develop/fork-a-luna) copies a single luna into another environment.
