DeleteDocument 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

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

DeleteDocument 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 DeleteDocument

CLI
AWS CLI

若要刪除文件

下列delete-document範例會刪除 Systems Manager 文件。

aws ssm delete-document \ --name "Example"

此命令不會產生輸出。

如需詳細資訊,請參閱 Systems Manager 使用者指南中的建立 Systems Manager 文件AWS

  • 如需API詳細資訊,請參閱 命令參考 DeleteDocument中的 。 AWS CLI

Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes an AWS SSM document asynchronously. * * @param documentName The name of the document to delete. * <p> * This method initiates an asynchronous request to delete an SSM document. * If an exception occurs, it handles the error appropriately. */ public void deleteDoc(String documentName) { DeleteDocumentRequest documentRequest = DeleteDocumentRequest.builder() .name(documentName) .build(); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { getAsyncClient().deleteDocument(documentRequest) .thenAccept(response -> { System.out.println("The SSM document was successfully deleted."); }) .exceptionally(ex -> { throw new CompletionException(ex); }).join(); }).exceptionally(ex -> { Throwable cause = (ex instanceof CompletionException) ? ex.getCause() : ex; if (cause instanceof SsmException) { throw new RuntimeException("SSM error: " + cause.getMessage(), cause); } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } }); try { future.join(); } catch (CompletionException ex) { throw ex.getCause() instanceof RuntimeException ? (RuntimeException) ex.getCause() : ex; } }
  • 如需API詳細資訊,請參閱 參考 DeleteDocument中的 。 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import { DeleteDocumentCommand, SSMClient } from "@aws-sdk/client-ssm"; import { parseArgs } from "node:util"; /** * Delete an SSM document. * @param {{ documentName: string }} */ export const main = async ({ documentName }) => { const client = new SSMClient({}); try { await client.send(new DeleteDocumentCommand({ Name: documentName })); console.log(`Document '${documentName}' deleted.`); return { Deleted: true }; } catch (caught) { if (caught instanceof Error && caught.name === "MissingParameter") { console.warn(`${caught.message}. Did you provide this value?`); } else { throw caught; } } };
  • 如需API詳細資訊,請參閱 參考 DeleteDocument中的 。 AWS SDK for JavaScript API

PowerShell
適用於 的工具 PowerShell

範例 1:此範例會刪除文件。如果命令成功,則不會輸出。

Remove-SSMDocument -Name "RunShellScript"
  • 如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 DeleteDocument中的 。

Python
SDK for Python (Boto3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class DocumentWrapper: """Encapsulates AWS Systems Manager Document actions.""" def __init__(self, ssm_client): """ :param ssm_client: A Boto3 Systems Manager client. """ self.ssm_client = ssm_client self.name = None @classmethod def from_client(cls): ssm_client = boto3.client("ssm") return cls(ssm_client) def delete(self): """ Deletes an AWS Systems Manager document. """ if self.name is None: return try: self.ssm_client.delete_document(Name=self.name) print(f"Deleted document {self.name}.") self.name = None except ClientError as err: logger.error( "Couldn't delete %s. Here's why: %s: %s", self.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 如需API詳細資訊,請參閱 DeleteDocument 中的 AWS SDK for Python (Boto3) API參考