# How do I change the way I connect to an environment without losing my existing stacks?

{% hint style="info" %}
Note that these steps apply to Docker environments only.
{% endhint %}

In the case of changing the way you connect to an environment (for example by moving from connecting to the Docker socket directly to using a socket proxy) you can move your stacks by:

{% stepper %}
{% step %}

#### Removing the current environment connection in Portainer

Under **Administration**, navigate to **Environment-related**, then to **Environments**. Select the environment you would like to remove and click **Remove.** The stacks in this environment will become orphaned, and therefore available to be re-associated once you re-add the environment in the next step.

<figure><img src="/files/S3H4jYvMmC4ZGZddwpvF" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### Adding a new environment using the new connection method

[Add the environment again](/admin/environments/add.md) using the new connection method.
{% endstep %}

{% step %}

#### Re-associating the orphaned stacks with the new environment

Within the environment that you want to associate your orphaned stacks with, click **Stacks** in the left hand menu. At the Stacks list, click on the three dots in the top right corner and select **Show all orphaned stacks**. Your stack list will then update to include any orphaned stacks.

<div align="left"><figure><img src="/files/zZXBZlZ0OEXu2cPLXeS3" alt=""><figcaption></figcaption></figure></div>

Click into the stack that you want to recover, and select **Associate.**

<figure><img src="/files/tD1qZ7IGoCGhNR8lMXCU" alt=""><figcaption></figcaption></figure>

Your stack will now appear in your stack list with total control. Repeat this process for each stack you want to re-associate.
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.portainer.io/faqs/troubleshooting/agents-and-environment-management/how-do-i-change-the-way-i-connect-to-an-environment-without-losing-my-existing-stacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
