---
title: "Migrate and Overwrite Content in the Same Stack"
description: "Migrate and overwrite existing content in the same Contentstack stack using the CLI entries module, including assets and dependencies."
url: "https://www.contentstack.com/docs/headless-cms/cli-migrate-and-overwrite-content-in-the-same-stack"
product: "Contentstack"
doc_type: "guide"
audience:
  - developers
  - admins
version: "current"
last_updated: "2026-08-06"
---

# Migrate and Overwrite Content in the Same Stack

Use this approach when the same stack already contains content, and you want to overwrite existing data instead of creating duplicates.

This method imports entries and their dependencies, including assets, extensions, marketplace apps, and taxonomies, using the entries module, ensuring content remains consistent across the stack.

**Warning:** The export writes content flat into the folder you name. It does not create a <branch-uid>/ subfolder, even when you pass \--branch. Use the same path for the export and the import, and use a separate \--data-dir per branch, because exporting a second branch into the same folder overwrites the first with no error.

If you are importing an export produced by CLI V1, that layout did nest content under a branch subfolder. Point \--data-dir at the subfolder itself, for example ./export/main, because the automatic branch detection that V1 relied on has been removed.

## Prerequisites

Before you begin, ensure the following:

*   You have exported content from the stack to a local folder.
*   You are authenticated using the CLI:
    
    ```
    csdx auth:login
    ```
    
*   You have the **Stack API key** of the target stack.
*   You have the necessary [permissions](/docs/headless-cms/types-of-roles).

## Step 1: Export Content (If Not Already Exported)

This command exports content from your stack.

```
csdx cm:stacks:export -k <stack_api_key> --data-dir ./export --branch main
```

Replace <stack\_api\_key> with your actual stack API key.

The export writes content directly into the folder you named, with no per-branch subfolder. Use that same path in the next steps.

## Step 2: Run Import Setup

The import-setup command generates mapper files and creates a backup of the existing entries in your stack.

```
csdx cm:stacks:import-setup -k <stack_api_key> --data-dir ./export --module entries
```

**Replace:**

*   <stack\_api\_key> with your stack API key.
*   ./export with your actual export path if different.

After the command runs, the CLI displays the backup folder path.

**Example output:**

The backup folder has been created at './\_backup\_123'

Copy the backup path shown in your terminal. Use this path in the next step.

## Step 3: Import with Overwrite

This command imports the content and replaces existing entries.

```
csdx cm:stacks:import \
-k <stack_api_key> \
--data-dir ./export \
--backup-dir ./backup_123 \
--replace-existing \
--module entries
```

**Replace:**

*   <stack\_api\_key> with your stack API key.
*   ./backup\_123 with the exact backup path generated in Step 2.
*   ./export if your export path is different.

## Branch-Specific Import

If importing into a specific branch, use:

*   \--branch <branch\_name>
*   \--branch-alias <alias\_name>

## Data Overwrite Considerations

Overwriting updates existing entries in the stack.

*   The backup created during import-setup is your recovery reference.
*   Always use the exact backup directory path generated by the CLI.
*   Do not manually modify backup files.

If you need to revert changes, use the backup folder generated during [Step 2](/docs/headless-cms/cli-migrate-and-overwrite-content-in-the-same-stack#step-2-run-import-setup) to restore previous content.