

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# `DeleteVpc`搭配使用 Amazon SDK 或 CLI
<a name="example_ec2_DeleteVpc_section"></a>

以下代码示例显示了如何使用`DeleteVpc`。

操作示例是大型程序的代码摘录，必须在上下文中运行。您可以在以下代码示例中查看此操作的上下文：
+  [创建基本虚拟专用网络](example_vpc_GettingStartedCLI_section.md) 
+  [图形数据库入门](example_ec2_GettingStarted_064_section.md) 
+  [具有 IP 地址管理功能的虚拟专用网络](example_vpc_GettingStartedIpam_section.md) 
+  [带有私有服务器的虚拟专用网络](example_vpc_GettingStartedPrivate_section.md) 
+  [使用网络对等连接](example_ec2_GettingStarted_015_section.md) 

------
#### [ CLI ]

**Amazon CLI**  
**删除 VPC**  
此示例删除指定的 VPC。如果命令成功，则不返回任何输出。  
命令:  

```
aws ec2 delete-vpc --vpc-id {{vpc-a01106c2}}
```
+  有关 API 的详细信息，请参阅*Amazon CLI 命令参考[DeleteVpc](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/delete-vpc.html)中的*。

------
#### [ PHP ]

**适用于 PHP 的 SDK**  
 还有更多 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/ec2#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    /**
     * @param string $vpcId
     * @return void
     */
    public function deleteVpc(string $vpcId)
    {
        try {
            $this->ec2Client->deleteVpc([
                "VpcId" => $vpcId,
            ]);
        }catch(Ec2Exception $caught){
            echo "There was a problem deleting the VPC: {$caught->getAwsErrorMessage()}\n";
            throw $caught;
        }
    }
```
+  有关 API 的详细信息，请参阅 *适用于 PHP 的 Amazon SDK API 参考[DeleteVpc](https://docs.amazonaws.cn/goto/SdkForPHPV3/ec2-2016-11-15/DeleteVpc)中的*。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：此示例删除指定的 VPC。除非您还指定了 Force 参数，否则在操作继续之前，系统会提示您进行确认。**  

```
Remove-EC2Vpc -VpcId vpc-12345678
```
**输出**：  

```
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-EC2Vpc (DeleteVpc)" on Target "vpc-12345678".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
```
+  有关 API 的详细信息，请参阅 [ DeleteVpc ](https://docs.amazonaws.cn/powershell/v4/reference) *Amazon Tools for PowerShell Cmdlet 参考文档 (V4)。*

**适用于 PowerShell V5 的工具**  
**示例 1：此示例删除指定的 VPC。除非您还指定了 Force 参数，否则在操作继续之前，系统会提示您进行确认。**  

```
Remove-EC2Vpc -VpcId vpc-12345678
```
**输出**：  

```
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-EC2Vpc (DeleteVpc)" on Target "vpc-12345678".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
```
+  有关 API 的详细信息，请参阅 [ DeleteVpc ](https://docs.amazonaws.cn/powershell/v5/reference) *Amazon Tools for PowerShell Cmdlet 参考文档 (V5)。*

------
#### [ Python ]

**适用于 Python 的 SDK（Boto3）**  
 还有更多 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/ec2#code-examples)中查找完整示例，了解如何进行设置和运行。

```
class VpcWrapper:
    """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) Amazon Virtual Private Cloud actions."""

    def __init__(self, ec2_client: boto3.client):
        """
        Initializes the VpcWrapper with an EC2 client.

        :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level
                           access to AWS EC2 services.
        """
        self.ec2_client = ec2_client

    @classmethod
    def from_client(cls) -> "VpcWrapper":
        """
        Creates a VpcWrapper instance with a default EC2 client.

        :return: An instance of VpcWrapper initialized with the default EC2 client.
        """
        ec2_client = boto3.client("ec2")
        return cls(ec2_client)


    def delete(self, vpc_id: str) -> None:
        """
        Deletes the specified VPC.

        :param vpc_id: The ID of the VPC to delete.
        """
        try:
            self.ec2_client.delete_vpc(VpcId=vpc_id)
        except ClientError as err:
            logger.error(
                "Couldn't delete VPC %s. Here's why: %s: %s",
                vpc_id,
                err.response["Error"]["Code"],
                err.response["Error"]["Message"],
            )
            raise
```
+  有关 API 的详细信息，请参阅 Python *Amazon 开发工具包 (Boto3) API 参考[DeleteVpc](https://docs.amazonaws.cn/goto/boto3/ec2-2016-11-15/DeleteVpc)中的内容。*

------
#### [ SAP ABAP ]

**适用于 SAP ABAP 的 SDK**  
 还有更多 GitHub。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/sap-abap/services/ec2#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    TRY.
        lo_ec2->deletevpc( iv_vpcid = iv_vpc_id ).
        MESSAGE 'Deleted VPC.' TYPE 'I'.
      CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception).
        DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|.
        MESSAGE lv_error TYPE 'E'.
    ENDTRY.
```
+  有关 API 的详细信息，请参阅*Amazon 适用[DeleteVpc](https://docs.amazonaws.cn/sdk-for-sap-abap/v1/api/latest/index.html)于 SAP 的 SDK ABAP API 参考*文档。

------

有关 S Amazon DK 开发者指南和代码示例的完整列表，请参阅[使用 Amazon SDK 创建 Amazon EC2 资源](sdk-general-information-section.md)。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。