export const metadata = {
  title: `Cloud Best Practices: Update Medusa Application`,
}

# {metadata.title}

In this guide, you'll learn how to update your Medusa application that is deployed on Cloud.

<Note title="Looking for the general update guide?">

Refer to the [Update guide](!docs!/learn/update) to learn how to update your Medusa application.

</Note>

## Why Update Your Medusa Application?

Medusa regularly releases new versions of the core application with bug fixes, performance improvements, and new features.

It's highly recommended to always keep your Medusa application up to date. You'll benefit from the latest improvements and security patches.

---

## Update Your Deployed Medusa Application

This section provides best practices for updating your Medusa application on Cloud and how to update your application in a safe manner.

### Don't Push Directly to Production

It's **strongly discouraged** to push a new update directly to your production branch. While Medusa updates are generally safe and stable, it's always a good practice to test the update in a separate environment before deploying it to production.

By using a separate environment, you mitigate any unexpected issues that may arise during the update process. This way, you ensure that your customizations and configurations work correctly with the new version of Medusa.

The **recommended** approach is to [create a long-lived environment](../environments/long-lived/page.mdx#create-a-long-lived-environment), such as Staging. You can push the new update and test it thoroughly in a production-like environment before deploying it to production.

<Note title="Tip">

Each environment has its own database, so you can safely update other environments and make data changes without affecting the production environment.

</Note>

![Diagram showcasing process of deploying to staging then production](https://res.cloudinary.com/dza7lstvk/image/upload/v1750334010/Cloud/deploy-to-staging_d4oyys.jpg)

Alternatively, you can open a pull request to the production branch, which will [create a preview environment](../environments/preview/page.mdx#preview-environments). The preview environment allows you to test the update before merging it into the production branch.

![Diagram showcasing process of deploying to preview then production](https://res.cloudinary.com/dza7lstvk/image/upload/v1750334010/Cloud/deploy-to-preview_plpz9z.jpg)

### Ensure Custom Migrations Have Rollback Methods

When creating custom migrations, it's important to include both `up` and `down` methods. The `up` method should define the changes to be made to the database schema, while the `down` method should reverse those changes.

By including both methods, you can easily roll back database changes if you need to revert the application's update.

If you use the [db\:generate](!resources!/medusa-cli/commands/db#dbgenerate) command to generate migrations, it will automatically create both `up` and `down` methods for you. If you're creating the migrations manually, you can learn more in the [Migrations](!docs!/learn/fundamentals/data-models/write-migration) guide.

### Update Your Medusa Application

To update your Medusa application deployed on Cloud:

1. Update your Medusa application locally either on the long-lived environment's branch (for example, Staging) or in a new branch that will be used to create a pull request.
    - To learn how to update your Medusa application, refer to the [Update guide](!docs!/learn/update). It includes all the details you need to update the Medusa application locally.
2. Once you're done updating your application locally, push the changes in `package.json`, lock file (for example, `yarn.lock`), and any other affected files to the branch.
    - If you deployed a storefront alongside your Medusa application, both the backend and storefront will be redeployed when you push changes.
    - If you're using a long-lived environment, Medusa will automatically deploy the changes to the branch's environment.
    - If you're using a new branch to create a pull request, open the pull request to the production branch. This will create a preview environment that you can use to test the update before merging it into the production branch.

Then, you can access the environment's [live deployment](../deployments/access/page.mdx) to test the update. You can also use the [build](../logs/page.mdx#view-build-logs) and [runtime](../logs/page.mdx#view-runtime-logs) logs of the environment to debug any issues that occur during the update process or testing.

### Push Update to Production

Once you've thoroughly tested the update in your long-lived or preview environment, you can merge the changes into your production branch.

Medusa will automatically deploy the changes to the production environment. This will update the production's code and database.

---

## Rollback Medusa Application Update

If you encounter any issues after updating your deployed Medusa application, you can [redeploy a previous deployment](../deployments/page.mdx#redeploy-a-deployment) to roll back the code changes made by the update.

However, redeploying a previous deployment will not roll back the database changes made by the update. If you need to roll back the database changes, contact support for assistance.
