55## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
66
77defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do
8- alias RabbitMQ.CLI.Core.DocGuide
8+ alias RabbitMQ.CLI.Core . { DocGuide , Users }
99
1010 @ behaviour RabbitMQ.CLI.CommandBehaviour
1111
12- def switches ( ) , do: [ if_empty: :boolean , if_unused: :boolean , timeout: :integer ]
12+ def switches ( ) , do: [ if_empty: :boolean , if_unused: :boolean , force: :boolean , timeout: :integer ]
1313 def aliases ( ) , do: [ e: :if_empty , u: :if_unused , t: :timeout ]
1414
1515 def merge_defaults ( args , opts ) do
1616 {
1717 args ,
18- Map . merge ( % { if_empty: false , if_unused: false , vhost: "/" } , opts )
18+ Map . merge ( % { if_empty: false , if_unused: false , force: false , vhost: "/" } , opts )
1919 }
2020 end
2121
@@ -46,37 +46,49 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do
4646 vhost: vhost ,
4747 if_empty: if_empty ,
4848 if_unused: if_unused ,
49+ force: force ,
4950 timeout: timeout
5051 } ) do
5152 ## Generate queue resource name from queue name and vhost
5253 queue_resource = :rabbit_misc . r ( vhost , :queue , qname )
54+ user = if force , do: Users . internal_user , else: Users . cli_user
5355 ## Lookup a queue on broker node using resource name
5456 case :rabbit_misc . rpc_call ( node , :rabbit_amqqueue , :lookup , [ queue_resource ] ) do
5557 { :ok , queue } ->
5658 ## Delete queue
57- :rabbit_misc . rpc_call (
58- node ,
59- :rabbit_amqqueue ,
60- :delete_with ,
61- [ queue , if_unused , if_empty , "cli_user" ] ,
62- timeout
63- )
59+ case :rabbit_misc . rpc_call ( node ,
60+ :rabbit_amqqueue ,
61+ :delete_with ,
62+ [ queue , if_unused , if_empty , user ] ,
63+ timeout
64+ ) do
65+ { :ok , _ } = ok -> ok
66+
67+ { :badrpc , { :EXIT , { :amqp_error , :resource_locked , _ , :none } } } ->
68+ { :error , :protected }
69+
70+ other_error -> other_error
71+ end
6472
6573 { :error , _ } = error ->
6674 error
6775 end
6876 end
6977
78+ def output ( { :error , :protected } , _options ) do
79+ { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "The queue is locked or protected from deletion" }
80+ end
81+
7082 def output ( { :error , :not_found } , _options ) do
71- { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "Queue not found" }
83+ { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "No such queue was found" }
7284 end
7385
7486 def output ( { :error , :not_empty } , _options ) do
75- { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "Queue is not empty" }
87+ { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "The queue is not empty" }
7688 end
7789
7890 def output ( { :error , :in_use } , _options ) do
79- { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "Queue is in use" }
91+ { :error , RabbitMQ.CLI.Core.ExitCodes . exit_usage ( ) , "The queue is in use" }
8092 end
8193
8294 def output ( { :ok , qlen } , _options ) do
@@ -103,14 +115,15 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DeleteQueueCommand do
103115 Enum . join ( Enum . concat ( [ if_empty_str , if_unused_str ] ) , "and " ) <> "..."
104116 end
105117
106- def usage ( ) , do: "delete_queue [--vhost <vhost>] <queue_name> [--if-empty|-e] [--if-unused|-u]"
118+ def usage ( ) , do: "delete_queue [--vhost <vhost>] <queue_name> [--if-empty|-e] [--if-unused|-u] [--force] "
107119
108120 def usage_additional ( ) do
109121 [
110122 [ "--vhost" , "Virtual host name" ] ,
111123 [ "<queue_name>" , "name of the queue to delete" ] ,
112124 [ "--if-empty" , "delete the queue if it is empty (has no messages ready for delivery)" ] ,
113- [ "--if-unused" , "delete the queue only if it has no consumers" ]
125+ [ "--if-unused" , "delete the queue only if it has no consumers" ] ,
126+ [ "--force" , "delete the queue even if it is protected" ]
114127 ]
115128 end
116129
0 commit comments