Skip to content

Commit 5c5a507

Browse files
authored
Add script to remove all RabbitMQ queues and exchanges
This script connects to a RabbitMQ server and removes all queues and exchanges, excluding default ones. It requires authentication and takes command line arguments for connection details.
1 parent 07423e9 commit 5c5a507

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

http_utils/clean_all.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
__author__ = 'gabriele'
2+
3+
import base64
4+
import time
5+
import datetime
6+
import json
7+
import sys
8+
9+
import urllib.request
10+
11+
12+
### This script removes all the queues, so be careful!!!
13+
14+
def print_time(step):
15+
ts = time.time();
16+
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S');
17+
print(st + " - " + step)
18+
19+
20+
def get_auth(username, password):
21+
credentials = ('%s:%s' % (username, password))
22+
encoded_credentials = base64.b64encode(credentials.encode('ascii'))
23+
return 'Authorization', 'Basic %s' % encoded_credentials.decode("ascii")
24+
25+
26+
def call_api(rabbitmq_host, rabbitmq_port, vhost, user, password, context):
27+
print(
28+
" *** removing all queues from " + rabbitmq_host + ":" + rabbitmq_port + " vhost: " + vhost + " user: " + user + " password: " + password)
29+
p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
30+
p.add_password(None, "http://" + rabbitmq_host + ":" + rabbitmq_port + "/api/" + context, user, password)
31+
32+
auth_handler = urllib.request.HTTPBasicAuthHandler(p)
33+
opener = urllib.request.build_opener(auth_handler)
34+
35+
urllib.request.install_opener(opener)
36+
37+
req = urllib.request.Request("http://" + rabbitmq_host + ":" + rabbitmq_port + "/api/" + context,
38+
method='GET')
39+
40+
res = urllib.request.urlopen(req, timeout=5)
41+
42+
print_time(" *** response done, loading json")
43+
items = json.load(res)
44+
for itm in items:
45+
if (itm['name'] == "(AMQP default)" or itm['name'] == "amq.direct" or
46+
itm['name'].startswith("amq.") or itm['name'] == ""):
47+
print_time(" *** skipping " + itm['name'])
48+
continue
49+
50+
print_time(" *** removing " + itm['name'])
51+
52+
request_del = urllib.request.Request(
53+
"http://" + rabbitmq_host + ":" + rabbitmq_port + "/api/" + context + "/" + vhost + "/" + itm[
54+
'name'], method='DELETE')
55+
urllib.request.urlopen(request_del, timeout=5)
56+
print_time(" *** removed " + itm['name'])
57+
58+
59+
if __name__ == '__main__':
60+
rabbitmq_host = sys.argv[1]
61+
rabbitmq_port = sys.argv[2]
62+
if len(sys.argv) > 3:
63+
vhost = sys.argv[3]
64+
else:
65+
vhost = "%2f"
66+
user = "guest"
67+
if len(sys.argv) > 4:
68+
user = sys.argv[4]
69+
70+
password = "guest"
71+
if len(sys.argv) > 5:
72+
password = sys.argv[5]
73+
74+
call_api(rabbitmq_host, rabbitmq_port, vhost, user, password, "queues")
75+
print_time(" *** all queues removed")
76+
call_api(rabbitmq_host, rabbitmq_port, vhost, user, password, "exchanges")

0 commit comments

Comments
 (0)