Salesforce(CRM・顧客管理システム)の組織に対して、destructiveChanges.xml(削除指示リスト)による削除・デプロイワークフロー(デプロイ:システムへの配置)を実行します。 **次のような場合に使用:** - ユーザーがカスタムオブジェクト、フィールド、Apexクラス、フロー、またはその他のメタデータコンポーネント(システム構成情報)を組織から削除・削除したいと要望する場合 - リリース(公開)の一部として「破壊的デプロイ」(既存のコンポーネント削除を伴うデプロイ)や削除を実行する場合 実行前にバリデーション(検証)を行い、本番環境への作用を明示的な確認で制限します。 **使用しない場合:** - ローカルファイルの削除(Bashを使用) - 新規コンポーネントのデプロイ(platform-metadata-deployを使用)
Execute the destructiveChanges.xml delete-and-deploy workflow against a Salesforce org. TRIGGER when the user asks to delete/remove a custom object, field, Apex class, flow, or any metadata component FROM an org, or to perform a 'destructive deploy' / removal as part of a release. Validates first and gates production with explicit confirmation. DO NOT TRIGGER for local file deletion (use Bash), or for net-new deploys (use platform-metadata-deploy).
Salesforce組織に対するメタデータ削除を、destructiveChanges(削除指定)マニフェストを通じて実行します。3段階のプロセス(範囲決定 → 検証 → 実行)で進め、本番環境にはより厳密なチェックを適用します。
ユーザーに確認するか、文脈から判断して、どのコンポーネントを削除するかを決めます。各コンポーネントについて以下を記録します:
CustomObject、CustomField、ApexClass、Flow、PermissionSet)Project__c、Account.Status__c、MyController)マニフェストを生成する前に、ローカルプロジェクト内で各コンポーネントへの参照を検索します。force-app/ ディレクトリに対してGrepコマンドを使用:
grep -rn "<componentApiName>" force-app/ --include='*.cls' --include='*.trigger' --include='*.xml' --include='*.js' --include='*.html'
参照が見つかった場合:
destructiveChanges.xml を生成するmanifest/destructiveChangesPre.xml(デプロイ前の削除用)または manifest/destructiveChangesPost.xml(デプロイ後の削除用)に書き込みます。Salesforceの標準的なメタデータ形式を使用:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Project__c</members>
<members>OldThing__c</members>
<name>CustomObject</name>
</types>
<types>
<members>Account.Status__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>
sfdx-project.json の sourceApiVersion からAPIバージョンを取得します。
コンポーネントをメタデータ種別ごとにグループ化(種別ごとに1つの <types> ブロック)します。ネームスペース付きのフィールドは Object.Field という記法を使用します。
削除デプロイを実行する前に、必ず 検証を行います:
sf project deploy validate \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--manifest manifest/package.xml \
--target-org <alias> \
--test-level RunLocalTests \
--json
(デプロイ後削除の場合は --post-destructive-changes を使用)
package.xml が存在しない場合、その横に空のファイルを作成します(削除のみのデプロイにもパッケージ記述子が必要):
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>
検証が失敗した場合、エラーを表示して 中止 します。よくある失敗パターン:
削除を実行する前に、対象が本番環境かどうかを確認します。確実な判定方法は、デプロイフック(本番|サンドボックス|スクラッチ|試用|不明 を返す)と同じ分類器を使うことです:
"${CLAUDE_PLUGIN_ROOT}/scripts/sf-deploy-gate" bucket <alias>
unknown は組織の種別を確認できなかった場合です(通常、組織レコードが不完全)。ユーザーに明確に伝えます:デプロイゲートは削除を許可し、保存時の自動デプロイはその組織ではオフのままであり、sf org login web で再認証すれば分類が通常復旧します。
分類器が production を返した場合:
platform-quick-deploy に倣う)--purge-on-delete を拒否PreToolUseフック(sf-deploy-gate destructive)は既に本番環境への削除コマンドをブロック — その拒否をユーザーに伝え、回避策を使わないこと。
sf project deploy start \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--manifest manifest/package.xml \
--target-org <alias> \
--json \
--wait 30
--purge-on-delete はユーザーが明確に完全削除を指示した場合にのみ追加します(ごみ箱をスキップ)。
削除デプロイが成功した後:
sf project retrieve start --metadata <Type>:<Name> の使用を勧めない(コンポーネントはもう存在しない) — 代わりに、ローカルソースをクリーンアップすることを提案:# 削除済みのローカルファイルを削除して、ソースの追跡を正確に保つ
rm -rf force-app/main/default/<path-to-component>
--purge-on-delete を追加しない--ignore-errors を使わないsfdx-project.json のAPIバージョンを使用する。ハードコード値を使わないrequired="true" 属性のフィールド、または RecordType ピックリスト値で使用されているフィールドを削除する場合、連鎖削除の影響を実行前に伝えるCoordinate metadata deletion against a Salesforce org via the destructiveChanges manifest. Runs in three phases: scope → validate → execute, with stricter guardrails for production.
Ask the user (or infer from context) which components to delete. For each, capture:
CustomObject, CustomField, ApexClass, Flow, PermissionSet)Project__c, Account.Status__c, MyController)Before generating the manifest, scan the local project for references to each component. Use Grep over force-app/:
grep -rn "<componentApiName>" force-app/ --include='*.cls' --include='*.trigger' --include='*.xml' --include='*.js' --include='*.html'
If references are found:
destructiveChanges.xmlWrite to manifest/destructiveChangesPre.xml (for pre-deploy deletion) or manifest/destructiveChangesPost.xml (for post-deploy deletion). Use the standard Salesforce metadata format:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Project__c</members>
<members>OldThing__c</members>
<name>CustomObject</name>
</types>
<types>
<members>Account.Status__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>
Use the API version from sfdx-project.json's sourceApiVersion.
Group components by metadata type (one <types> block per type). For namespaced fields, use Object.Field notation.
ALWAYS validate before executing a destructive deploy:
sf project deploy validate \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--manifest manifest/package.xml \
--target-org <alias> \
--test-level RunLocalTests \
--json
(For post-destructive: use --post-destructive-changes.)
If package.xml doesn't exist, create an empty one alongside (deletion-only deploy needs a package descriptor):
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<version>62.0</version>
</Package>
If validation fails, surface errors and STOP. Common failure modes:
Confirm whether the target is production before executing. The reliable check is the gate's classifier, which resolves the org the same way the deploy hook does (returns production|sandbox|scratch|trial|unknown):
"${CLAUDE_PLUGIN_ROOT}/scripts/sf-deploy-gate" bucket <alias>
unknown means the org type could not be confirmed (usually an incomplete org record). Say so plainly: the deploy gate still allows the deploy, auto-deploy on save stays off for that org, and re-authenticating with sf org login web usually restores the classification.
If the classifier returns production:
platform-quick-deploy)--purge-on-delete unless the user types it explicitlyThe PreToolUse hook (sf-deploy-gate destructive) will already block bare destructive commands against prod — surface that denial to the user, do not work around it.
sf project deploy start \
--pre-destructive-changes manifest/destructiveChangesPre.xml \
--manifest manifest/package.xml \
--target-org <alias> \
--json \
--wait 30
Add --purge-on-delete only if the user explicitly asked to permanently delete (skip the recycle bin).
After a successful destructive deploy:
sf project retrieve start --metadata <Type>:<Name> is NOT useful (component is gone) — instead suggest cleaning up the local source:# Remove the now-deleted local files to keep source tracking accurate
rm -rf force-app/main/default/<path-to-component>
--purge-on-delete without explicit user request--ignore-errors on a destructive deploysfdx-project.json, not a hardcoded valuerequired="true" or that's used in RecordType picklist values, surface the cascade impact before proceeding原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。