刪除儲存體帳戶

 

Delete Storage Account 非同步作業會刪除指定的儲存體帳戶。

要求

Delete Storage Account 要求可能會指定,如下所示。 取代 <subscription-id> 與您的訂閱識別碼和 <service-name> 的儲存體帳戶名稱。

方法

要求 URI

刪除

https://management.core.windows.net/<subscription-id>/services/storageservices/<service-name>

您必須確定對管理服務的要求是安全。 如需詳細資訊,請參閱 驗證服務管理要求。

URI 參數

無。

要求標頭

下表描述要求標頭。

要求標頭

描述

x-ms-version

必要的。 指定要用於此要求之作業的版本。 此標頭的值必須設為 2011-06-01 或更高版本。 如需版本設定標頭的詳細資訊,請參閱 服務管理版本設定。

要求本文

無。

回應

回應包括 HTTP 狀態碼、 一組回應標頭和回應主體。

狀態碼

成功的作業會傳回狀態碼 200 (確定)。 如需狀態碼的詳細資訊,請參閱 服務管理狀態和錯誤碼。

回應標頭

這項作業的回應包括下列標頭。 回應也可能包括其他標準 HTTP 標頭。 所有標準標頭都符合 HTTP/1.1 通訊協定規格https://go.microsoft.com/fwlink/?linkid=150478。

回應標頭

描述

x-ms-request-id

值可唯一識別對管理服務所提出的要求。 非同步作業,您可以呼叫 取得作業狀態 要判斷是否已完成作業的標頭的值,已失敗,或仍在進行中。

回應本文

無。

註解

使用 Delete Storage Account 作業,以刪除儲存體帳戶的名稱。 無法復原已刪除的儲存體帳戶中儲存的資料,並可供其他使用者採用已刪除的儲存體帳戶名稱。

刪除儲存體帳戶之前,您必須先刪除所有 OS 映像、 VM 映像,以及位於帳戶中的磁碟。 非同步作業會用來從儲存體帳戶刪除成品。 您可以使用 取得作業狀態 作業,以便確保作業已完成再嘗試刪除儲存體帳戶。 它可能需要 15 分鐘的時間完成後,才能刪除儲存體帳戶的所有作業。

範例

下列範例程式會採用訂閱識別碼、 相關聯的管理憑證指紋、 作業版本,和儲存體帳戶名稱,以及呼叫 Delete Storage Account 移除命名的儲存體帳戶的作業。 初始化 Version 常數是版本標頭字串, SubscriptionId 訂閱的 GUID 識別碼 Thumbprint 您管理憑證的指紋值,和 ServiceName 執行範例程式碼要刪除的儲存體帳戶的名稱。

using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.Xml.Linq;

public class Program
{
    // Set these constants with your values to run the sample.
    private const string Version = "2011-10-01";
    private const string Thumbprint = "management-certificate-thumbprint";
    private const string SubscriptionId = "subscription-id-guid";
    private const string ServiceName = "storage-account-name";

    /// <summary>
    /// Gets or sets the certificate that matches the Thumbprint value.
    /// </summary>
    private static X509Certificate2 Certificate { get; set; }

    static void Main(string[] args)
    {
        try
        {
            Certificate = GetCertificate(Thumbprint);

            DeleteStorageAccount(SubscriptionId, ServiceName);
            Console.WriteLine("Storage Account {0} deleted.", ServiceName);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in Main:");
            Console.WriteLine(ex.Message);
        }

        Console.Write("Press any key to continue:");
        Console.ReadKey();
    }

    /// <summary>
    /// Calls the Delete Storage Account operation in the Service Management
    /// REST API for the specified subscription and storage account name.
    /// Throws an ApplicationException on status code results other than OK.
    /// </summary>
    /// <param name="subscriptionId">The subscription identifier.</param>
    /// <param name="serviceName">The name of the storage account to delete.</param>
    private static void DeleteStorageAccount(
        string subscriptionId,
        string serviceName)
    {
        string uriFormat = "https://management.core.windows.net/{0}/services/storageservices/{1}";
        Uri uri = new Uri(String.Format(uriFormat, subscriptionId, serviceName));

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = "DELETE";
        request.Headers.Add("x-ms-version", Version);
        request.ClientCertificates.Add(Certificate);
        request.ContentType = "application/xml";

        XDocument responseBody = null;
        HttpStatusCode statusCode = HttpStatusCode.Unused;
        HttpWebResponse response;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
            // GetResponse throws a WebException for 4XX and 5XX status codes
            response = (HttpWebResponse)ex.Response;
        }

        try
        {
            statusCode = response.StatusCode;
            if (response.ContentLength > 0)
            {
                using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                {
                    responseBody = XDocument.Load(reader);
                }
            }
        }
        finally
        {
            response.Close();
        }

        if (!statusCode.Equals(HttpStatusCode.OK))
        {
            throw new ApplicationException(string.Format(
                "Call to {0} returned an error:{1}Status Code: {2} ({3}):{1}{4}",
                uri.ToString(),
                Environment.NewLine,
                (int)statusCode,
                statusCode,
                responseBody.ToString(SaveOptions.OmitDuplicateNamespaces)));
        }

        return;
    }

    /// <summary>
    /// Gets the certificate matching the thumbprint from the local store.
    /// Throws an ArgumentException if a matching certificate is not found.
    /// </summary>
    /// <param name="thumbprint">The thumbprint of the certificate to find.</param>
    /// <returns>The certificate with the specified thumbprint.</returns>
    private static X509Certificate2 GetCertificate(string thumbprint)
    {
        List<StoreLocation> locations = new List<StoreLocation> 
        { 
            StoreLocation.CurrentUser, 
            StoreLocation.LocalMachine 
        };

        foreach (var location in locations)
        {
            X509Store store = new X509Store("My", location);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection certificates = store.Certificates.Find(
                    X509FindType.FindByThumbprint, thumbprint, false);
                if (certificates.Count == 1)
                {
                    return certificates[0];
                }
            }
            finally
            {
                store.Close();
            }
        }

        throw new ArgumentException(string.Format(
            "A Certificate with Thumbprint '{0}' could not be located.",
            thumbprint));
    }
}

這個範例程式會產生結果如下所示 ︰

Storage Account myexamplestorage1 deleted.
Press any key to continue: