@@ -259,6 +259,145 @@ a node at `rabbitmq.local:15672`:
259259curl -u userename:pa$sw0rD -X DELETE http://rabbitmq.local:15672/api/vhosts/vh1
260260```
261261
262+ ## Deletion Protection {#deletion-protection}
263+
264+ A virtual host can be protected from deletion. Protected virtual hosts cannot be deleted
265+ until the protection is removed.
266+
267+ ### Using CLI Tools
268+
269+ ` rabbitmqctl enable_vhost_protection_from_deletion ` is the command that marks a virtual host
270+ as protected from deletion:
271+
272+ ``` bash
273+ rabbitmqctl enable_vhost_protection_from_deletion " vhost-name"
274+ ```
275+
276+ An attempt to delete the virtual host then will fail with a specific message:
277+
278+ ``` bash
279+ rabbitmqctl delete_vhost " vhost-name"
280+ # ...
281+ # => Error:
282+ # => Cannot delete this virtual host: it is protected from deletion. To lift the protection, inspect and update its metadata
283+ ```
284+
285+ To remove the protection, use ` rabbitmqctl disable_vhost_protection_from_deletion ` :
286+
287+ ``` bash
288+ # # removes virtual host deletion protection
289+ rabbitmqctl disable_vhost_protection_from_deletion " vhost-name"
290+ ```
291+
292+ with the protection removed, the virtual host can be deleted again:
293+
294+ ``` bash
295+ rabbitmqctl delete_vhost " vhost-name"
296+ # => Deleting vhost "vhost-name" ...
297+ ```
298+
299+ To see whether a virtual host is protected from deletion, use ` list_vhosts ` command with
300+ an extra column, ` protected_from_deletion ` :
301+
302+ ``` shell
303+ rabbitmqctl list_vhosts name tags default_queue_type metadata protected_from_deletion --formatter=pretty_table
304+ # => Listing vhosts ...
305+ # => ┌───────────────────────────┬─────────────────────────┐
306+ # => │ name │ protected_from_deletion │
307+ # => ├───────────────────────────┼─────────────────────────┤
308+ # => │ / │ false │
309+ # => ├───────────────────────────┼─────────────────────────┤
310+ # => │ vh1 │ true │
311+ # => ├───────────────────────────┼─────────────────────────┤
312+ # => │ vh2 │ false │
313+ # => └───────────────────────────┴─────────────────────────┘
314+ ```
315+
316+ ### Using HTTP API
317+
318+ A virtual host can be protected from deletion using the ` POST /api/vhosts/{name}/deletion/protection ` [ HTTP API] ( ./management ) endpoint
319+ where ` {name} ` is the name of the virtual host.
320+
321+ Here's an example that uses [ curl] ( https://curl.haxx.se/ ) to delete a virtual host ` vh1 ` by contacting
322+ a node at ` rabbitmq.local:15672 ` :
323+
324+ ``` bash
325+ curl -u userename:pa$sw0rD -X POST http://rabbitmq.local:15672/api/vhosts/vh1/deletion/protection
326+ ```
327+
328+ An attempt to delete the virtual host then will fail with a ` 412 Precondition Failed ` status:
329+
330+ ``` bash
331+ curl -sL -u guest:guest -X DELETE http://localhost:15672/api/vhosts/vh1/
332+ # => < HTTP/1.1 412 Precondition Failed
333+ ```
334+
335+ The body will include a specific error, similar to what CLI tools output:
336+
337+ ``` json
338+ {
339+ "error" : " precondition_failed" ,
340+ "reason" : " Refusing to delete virtual host 'vh1' because it is protected from deletion"
341+ }
342+ ```
343+
344+ To remove the protection, use ` DELETE /api/vhosts/{name}/deletion/protection ` :
345+
346+ ``` bash
347+ curl -u userename:pa$sw0rD -X POST http://rabbitmq.local:15672/api/vhosts/vh1/deletion/protection
348+ ```
349+
350+ with the protection removed, the virtual host can be deleted again:
351+
352+ ``` bash
353+ curl -vv -sL -u guest:guest -X DELETE http://localhost:15672/api/vhosts/
354+ # ...
355+ # => < HTTP/1.1 204 No Content
356+ ```
357+
358+ To see whether a virtual host is protected from deletion, use the ` GET /api/vhosts ` or ` GET /api/vhosts/{vhost} `
359+ endpoints and then inspec the ` metadata.protected_from_deletion ` response body field:
360+
361+ ``` bash
362+ curl -sL -u guest:guest -X GET http://localhost:15672/api/vhosts/vh1
363+ # => {
364+ # => "name": "vh1",
365+ # => "description": "",
366+ # => "tags": [],
367+ # => "default_queue_type": "classic",
368+ # => "protected_from_deletion": true,
369+ # => "metadata": {
370+ # => "description": "",
371+ # => "tags": [],
372+ # => "default_queue_type": "classic",
373+ # => "protected_from_deletion": true
374+ # => },
375+ # => "tracing": false,
376+ # => "cluster_state": {
377+ # => "rabbit@sunnyside": "running"
378+ # => }
379+ # => }
380+ ```
381+
382+ ### Definition Imports
383+
384+ If a virtual host is created via a [ definition file] ( ./definitions/ ) , adding a new metadata key, ` "protected_from_deletion" ` ,
385+ that is set to ` true ` , will mark the virtual host as protected when it is created:
386+
387+ ``` json
388+ {
389+ "name" : " protected" ,
390+ "description" : " " ,
391+ "metadata" : {
392+ "description" : " This virtual host is protected from deletion with a special metadata key" ,
393+ "tags" : [],
394+ "default_queue_type" : " classic" ,
395+ "protected_from_deletion" : true
396+ },
397+ "tags" : [],
398+ "default_queue_type" : " classic"
399+ }
400+ ```
262401
263402## Limits {#limits}
264403
0 commit comments