删除Domain CLI

SageMaker删除Domain时,有很多依赖,比如要先删除apps, user profiles, 这里写了个脚本,将DOMAIN IDREGION进行替换,然后运行并删除即可(建议在linux上面跑, mac上面有些语法可能不兼容):



#!/bin/bash

# Pass the SageMaker Domain ID and Region as parameters
DOMAIN_ID=d-qrkw60y9xlu9
REGION=us-west-2

# Check if DOMAIN_ID and REGION are provided
if [ -z "$DOMAIN_ID" ] || [ -z "$REGION" ]; then
  echo "Usage: $0 <DOMAIN_ID> <REGION>"
  exit 1
fi

# Step 1: List all user profiles in the domain
USER_PROFILES=$(aws sagemaker list-user-profiles --domain-id-equals $DOMAIN_ID --region $REGION --query "UserProfiles[].UserProfileName" --output text)

# Step 2: Delete all apps, spaces, and user profiles
for USER_PROFILE in $USER_PROFILES; do
  echo "Processing user profile: $USER_PROFILE"




 	SPACES=$(aws sagemaker list-spaces --domain-id-equals $DOMAIN_ID  --region $REGION --query 'Spaces[*].SpaceName' --output text)
  
  for SPACE in $SPACES; do
    echo "  Processing space: $SPACE"
    
    # List and delete all apps in the space
    APPS=$(aws sagemaker list-apps --domain-id $DOMAIN_ID --space-name $SPACE --region $REGION --query "Apps[].{AppName:AppName,AppType:AppType}" --output json)


	for ROW in $(echo "${APPS}" | jq -c '.[]'); do
	    APP_NAME=$(echo $ROW | jq -r '.AppName')
	    APP_TYPE=$(echo $ROW | jq -r '.AppType')
	    echo "  Deleting app: $APP_NAME of type: $APP_TYPE"
	    aws sagemaker delete-app --domain-id $DOMAIN_ID --space-name $SPACE --app-name $APP --region $REGION --app-type $APP_TYPE
	   
	  done

    
    for APP in $APPS; do
      echo "    Deleting app: $APP in space: $SPACE"
      aws sagemaker delete-app --domain-id $DOMAIN_ID --space-name $SPACE --app-name $APP --region $REGION
    done
    
    # Delete the space
    echo "  Deleting space: $SPACE"
    aws sagemaker delete-space --domain-id $DOMAIN_ID --space-name $SPACE --region $REGION
  done

  

  # Delete the user profile
  echo "  Deleting user profile: $USER_PROFILE"
  aws sagemaker delete-user-profile --domain-id $DOMAIN_ID --user-profile-name $USER_PROFILE --region $REGION
  sleep 5
done

# Step 3: Delete the SageMaker domain
echo "Deleting SageMaker domain: $DOMAIN_ID"
aws sagemaker delete-domain --domain-id $DOMAIN_ID --region $REGION

echo "SageMaker domain $DOMAIN_ID and all associated resources have been deleted."