

Versi 4 (V4) dari AWS SDK untuk .NET telah dirilis\$1

Untuk informasi tentang melanggar perubahan dan memigrasi aplikasi Anda, lihat [topik migrasi](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html).

 [https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Menghapus antrian Amazon SQS
<a name="DeleteSqsQueue"></a>

Contoh ini menunjukkan cara menggunakan antrean Amazon SQS AWS SDK untuk .NET untuk menghapus. Aplikasi menghapus antrian, menunggu hingga jumlah waktu tertentu untuk antrian hilang, dan kemudian menunjukkan daftar antrian yang tersisa.

Jika Anda tidak memberikan argumen baris perintah apa pun, aplikasi hanya menampilkan daftar antrian yang ada.

Bagian berikut menyediakan cuplikan dari contoh ini. [Kode lengkap untuk contoh](#DeleteSqsQueue-complete-code) ditampilkan setelah itu, dan dapat dibangun dan dijalankan apa adanya.

**Topics**
+ [Hapus antrian](#DeleteSqsQueue-delete-queue)
+ [Tunggu antrian hilang](#DeleteSqsQueue-wait)
+ [Tampilkan daftar antrian yang ada](#DeleteSqsQueue-list-queues)
+ [Kode lengkap](#DeleteSqsQueue-complete-code)
+ [Pertimbangan tambahan](#DeleteSqsQueue-additional)

## Hapus antrian
<a name="DeleteSqsQueue-delete-queue"></a>

Cuplikan berikut menghapus antrian yang diidentifikasi oleh URL antrian yang diberikan.

Contoh [di akhir topik ini](#DeleteSqsQueue-complete-code) menunjukkan cuplikan ini digunakan.

```
    //
    // Method to delete an SQS queue
    private static async Task DeleteQueue(IAmazonSQS sqsClient, string qUrl)
    {
      Console.WriteLine($"Deleting queue {qUrl}...");
      await sqsClient.DeleteQueueAsync(qUrl);
      Console.WriteLine($"Queue {qUrl} has been deleted.");
    }
```

## Tunggu antrian hilang
<a name="DeleteSqsQueue-wait"></a>

Cuplikan berikut menunggu proses penghapusan selesai, yang mungkin memakan waktu 60 detik.

Contoh [di akhir topik ini](#DeleteSqsQueue-complete-code) menunjukkan cuplikan ini digunakan.

```
    //
    // Method to wait up to a given number of seconds
    private static async Task Wait(
      IAmazonSQS sqsClient, int numSeconds, string qUrl)
    {
      Console.WriteLine($"Waiting for up to {numSeconds} seconds.");
      Console.WriteLine("Press any key to stop waiting. (Response might be slightly delayed.)");
      for(int i=0; i<numSeconds; i++)
      {
        Console.Write(".");
        Thread.Sleep(1000);
        if(Console.KeyAvailable) break;

        // Check to see if the queue is gone yet
        var found = false;
        ListQueuesResponse responseList = await sqsClient.ListQueuesAsync("");
        foreach(var url in responseList.QueueUrls)
        {
          if(url == qUrl)
          {
            found = true;
            break;
          }
        }
        if(!found) break;
      }
    }
```

## Tampilkan daftar antrian yang ada
<a name="DeleteSqsQueue-list-queues"></a>

Cuplikan berikut menunjukkan daftar antrian yang ada di wilayah klien SQS.

Contoh [di akhir topik ini](#DeleteSqsQueue-complete-code) menunjukkan cuplikan ini digunakan.

```
    //
    // Method to show a list of the existing queues
    private static async Task ListQueues(IAmazonSQS sqsClient)
    {
      ListQueuesResponse responseList = await sqsClient.ListQueuesAsync("");
      Console.WriteLine("\nList of queues:");
      foreach(var qUrl in responseList.QueueUrls)
        Console.WriteLine($"- {qUrl}");
    }
```

## Kode lengkap
<a name="DeleteSqsQueue-complete-code"></a>

Bagian ini menunjukkan referensi yang relevan dan kode lengkap untuk contoh ini.

### Referensi SDK
<a name="w2aac19c15c29c21c25b5b1"></a>

NuGet paket:
+ [AWSSDK.SQS](https://www.nuget.org/packages/AWSSDK.SQS)

Elemen pemrograman:
+ [Namespace Amazon.sqs](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/SQS/NSQS.html)

  Kelas [Amazon SQSClient](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/SQS/TSQSClient.html)
+ [Namespace Amazon.sqs.Model](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/SQS/NSQSModel.html)

  Kelas [ListQueuesResponse](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/SQS/TListQueuesResponse.html)

### Kodenya
<a name="w2aac19c15c29c21c25b7b1"></a>

```
using System;
using System.Threading;
using System.Threading.Tasks;
using Amazon.SQS;
using Amazon.SQS.Model;

namespace SQSDeleteQueue
{
  // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  // Class to update a queue
  class Program
  {
    private const int TimeToWait = 60;

    static async Task Main(string[] args)
    {
      // Create the Amazon SQS client
      var sqsClient = new AmazonSQSClient();

      // If no command-line arguments, just show a list of the queues
      if(args.Length == 0)
      {
        Console.WriteLine("\nUsage: SQSCreateQueue queue_url");
        Console.WriteLine("   queue_url - The URL of the queue you want to delete.");
        Console.WriteLine("\nNo arguments specified.");
        Console.Write("Do you want to see a list of the existing queues? ((y) or n): ");
        var response = Console.ReadLine();
        if((string.IsNullOrEmpty(response)) || (response.ToLower() == "y"))
          await ListQueues(sqsClient);
        return;
      }

      // If given a queue URL, delete that queue
      if(args[0].StartsWith("https://sqs."))
      {
        // Delete the queue
        await DeleteQueue(sqsClient, args[0]);
        // Wait for a little while because it takes a while for the queue to disappear
        await Wait(sqsClient, TimeToWait, args[0]);
        // Show a list of the remaining queues
        await ListQueues(sqsClient);
      }
      else
      {
        Console.WriteLine("The command-line argument isn't a queue URL:");
        Console.WriteLine($"{args[0]}");
      }
    }


    //
    // Method to delete an SQS queue
    private static async Task DeleteQueue(IAmazonSQS sqsClient, string qUrl)
    {
      Console.WriteLine($"Deleting queue {qUrl}...");
      await sqsClient.DeleteQueueAsync(qUrl);
      Console.WriteLine($"Queue {qUrl} has been deleted.");
    }


    //
    // Method to wait up to a given number of seconds
    private static async Task Wait(
      IAmazonSQS sqsClient, int numSeconds, string qUrl)
    {
      Console.WriteLine($"Waiting for up to {numSeconds} seconds.");
      Console.WriteLine("Press any key to stop waiting. (Response might be slightly delayed.)");
      for(int i=0; i<numSeconds; i++)
      {
        Console.Write(".");
        Thread.Sleep(1000);
        if(Console.KeyAvailable) break;

        // Check to see if the queue is gone yet
        var found = false;
        ListQueuesResponse responseList = await sqsClient.ListQueuesAsync("");
        foreach(var url in responseList.QueueUrls)
        {
          if(url == qUrl)
          {
            found = true;
            break;
          }
        }
        if(!found) break;
      }
    }


    //
    // Method to show a list of the existing queues
    private static async Task ListQueues(IAmazonSQS sqsClient)
    {
      ListQueuesResponse responseList = await sqsClient.ListQueuesAsync("");
      Console.WriteLine("\nList of queues:");
      foreach(var qUrl in responseList.QueueUrls)
        Console.WriteLine($"- {qUrl}");
    }
  }
}
```

## Pertimbangan tambahan
<a name="DeleteSqsQueue-additional"></a>
+ Panggilan `DeleteQueueAsync` API tidak memeriksa untuk melihat apakah antrian yang Anda hapus digunakan sebagai antrian huruf mati. Prosedur yang lebih canggih dapat memeriksa ini.
+ Anda juga dapat melihat daftar antrian dan hasil contoh ini di konsol [Amazon SQS](https://console.aws.amazon.com/sqs).