

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 分離和清除自訂映像資源
<a name="studio-updated-byoi-how-to-detach-from-domain"></a>

以下頁面提供如何使用 Amazon SageMaker AI 主控台或 AWS Command Line Interface () 分離自訂映像和清除相關資源的說明AWS CLI。

**重要**  
您必須先從網域分離自訂映像，才能從 SageMaker AI 映像存放區刪除映像。如果沒有，您可能會在檢視網域資訊或將新的自訂映像連接到網域時遇到錯誤。  
如果您載入自訂映像時遇到錯誤，請參閱[無法載入自訂映像](studio-updated-troubleshooting.md#studio-updated-troubleshooting-custom-image)。

## 分離和刪除自訂映像 (主控台)
<a name="studio-updated-byoi-how-to-detach-from-domain-console"></a>

以下提供如何使用主控台，從 SageMaker AI 分離自訂映像，以及清除自訂映像資源。

**從您的網域分離您的自訂映像**

1. 開啟 [SageMaker AI 主控台](https://console.aws.amazon.com/sagemaker)。

1. 展開**管理員組態**區段。

1. 在**管理員組態**下，選擇**網域**。

1. 從**網域**清單中，選取網域。

1. 開啟**環境**索引標籤。

1. 針對**個人 Studio 應用程式的自訂映像**，選取您要分離之映像的核取方塊。

1. 請選擇**分離**。

1. 請遵循指示來分離。

**刪除您的自訂映像**

1. 開啟 [SageMaker AI 主控台](https://console.aws.amazon.com/sagemaker)。

1. 如果尚未這樣做，請展開**管理員組態**區段。

1. 在**管理員組態**下，選擇**映像**。

1. 從**映像**清單中，選取您要刪除的映像。

1. 選擇 **刪除**。

1. 遵循指示從 SageMaker AI 刪除您的映像及其所有版本。

**從 Amazon ECR 刪除您的自訂映像和儲存庫**
**重要**  
這也會刪除此儲存庫中的任何容器映像和成品。

1. 開啟 [Amazon ECR 主控台](https://console.aws.amazon.com/ecr)。

1. 如果尚未這樣做，請展開左側導覽窗格。

1. 在**私有登錄檔**下，選擇**儲存庫**。

1. 選取您要刪除的儲存庫。

1. 選擇 **刪除**。

1. 請遵循指示來刪除。

## 分離和刪除自訂映像 (AWS CLI)
<a name="studio-updated-byoi-how-to-detach-from-domain-cli"></a>

下節顯示如何使用 AWS CLI分離自訂映像的範例。

1. 首先為後續的 AWS CLI 命令設定變數。

   ```
   ACCOUNT_ID=account-id
   REGION=aws-region
   APP_IMAGE_CONFIG=app-image-config
   SAGEMAKER_IMAGE_NAME=custom-image-name
   ```
   + `aws-region` 是 Amazon SageMaker AI 網域 AWS 區域 的 。您可以在任何 AWS 主控台頁面的右上角找到此項目。
   + `app-image-config` 是應用程式映像組態的名稱。使用下列 AWS CLI 命令列出 中的應用程式映像組態 AWS 區域。

     ```
     aws sagemaker list-app-image-configs \
            --region ${REGION}
     ```
   + `custom-image-name` 是自訂映像名稱。使用下列 AWS CLI 命令列出 中的映像 AWS 區域。

     ```
     aws sagemaker list-images \
            --region ${REGION}
     ```

1. 若要使用這些指示從您的網域分離映像和映像版本，您需要建立或更新網域組態 json 檔案。
**注意**  
如果已遵循[將自訂映像連接至您的網域](studio-updated-byoi-how-to-attach-to-domain.md)中的指示，您可能已使用名為 `update-domain.json` 的檔案來更新網域。  
如果沒有該檔案，您可以改為建立新的 json 檔案。

   建立名為 `update-domain.json` 的檔案，您將用來更新網域。

1. 若要刪除自訂映像，您需要將 `CustomImages` 保留空白，例如 `"CustomImages": []`。選擇下列其中一項，以檢視程式碼編輯器或 JupyterLab 的範例組態檔案。

------
#### [ Code Editor: update domain configuration file example ]

   程式碼編輯器的組態檔案範例，使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CodeEditorAppSettings.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CodeEditorAppSettings.html)。

   ```
   {
       "DomainId": "domain-id",
       "DefaultUserSettings": {
           "CodeEditorAppSettings": {
               "CustomImages": [
               ]
           }
       }
   }
   ```

------
#### [ JupyterLab: update domain configuration file example ]

   JupyterLab 的組態檔案範例，使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_JupyterLabAppSettings.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_JupyterLabAppSettings.html)。

   ```
   {
       "DomainId": "domain-id",
       "DefaultUserSettings": {
           "JupyterLabAppSettings": {
               "CustomImages": [
               ]
           }
       }
   }
   ```

------

   `domain-id` 是您映像連接至其中的網域 ID。使用下列命令列出您的網域。

   ```
   aws sagemaker list-domains \
         --region ${REGION}
   ```

1. 儲存檔案。

1. 使用[更新網域組態檔案 呼叫 update-domain](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/update-domain.html) AWS CLI `update-domain.json`。
**注意**  
您必須先刪除網域中的所有**應用程式**，然後才能更新自訂映像。您**不**需要刪除使用者設定檔或共用空間。如需刪除應用程式的指示，請選擇下列其中一個選項。  
如果您想要使用 SageMaker AI 主控台，請參閱[關閉網域中的 SageMaker AI 資源](sm-console-domain-resources-shut-down.md)。
如果您想要使用 AWS CLI，請使用 的步驟 1 到 3[刪除 Amazon SageMaker AI 網域 (AWS CLI)](gs-studio-delete-domain.md#gs-studio-delete-domain-cli)。

   ```
   aws sagemaker update-domain \
       --cli-input-json file://update-domain.json \
       --region ${REGION}
   ```

1. 刪除應用程式映像組態。

   ```
   aws sagemaker delete-app-image-config \
       --app-image-config-name ${APP_IMAGE_CONFIG}
   ```

1. 刪除自訂映像。這也會刪除所有映像版本。這不會刪除 Amazon ECR 容器映像和映像版本。若要這麼做，請使用下列選用步驟。

   ```
   aws sagemaker delete-image \
       --image-name ${SAGEMAKER_IMAGE_NAME}
   ```

1. (選用) 刪除您的 Amazon ECR 資源。下列清單提供 AWS CLI 命令，以取得下列步驟的 Amazon ECR 資源資訊。

   1. 為後續 AWS CLI 命令設定變數。

      ```
      ECR_REPO_NAME=ecr-repository-name
      ```

      `ecr-repository-name` 是 Amazon Elastic Container Registry 儲存庫的名稱。

      若要列出儲存庫的詳細資訊，請使用下列命令。

      ```
      aws ecr describe-repositories \
              --region ${REGION}
      ```

   1. 從 Amazon ECR 刪除您的儲存庫。
**重要**  
這也會刪除此儲存庫中的任何容器映像和成品。

      ```
      aws ecr delete-repository \
            --repository-name ${ECR_REPO_NAME} \
            --force \
            --region ${REGION}
      ```