# Delete unused subnets

```bash
curl -O https://wissen.woitzel.eu/unused_subnets.py && python3 unused_subnets.py -k
```

#### What it does

Connects to one Prism Central, lists every subnet (with its cluster) that no VM NIC points to and optionally deletes the ones you mark. Python 3 only, no extra modules.

#### Usage

```bash
python3 unused_subnets.py [--host HOST] [--user USER] [-k] [--csv FILE] [--list-only]

# ask for host, user and password
python3 unused_subnets.py

# everything on the command line
python3 unused_subnets.py --host pc.example.local --user admin -k

# read-only: list and save as CSV, never offer to delete
python3 unused_subnets.py --host pc.example.local --user admin -k --list-only --csv unused.csv
```

#### Options

<table id="bkmrk-optiondescription---"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td>`--host HOST`</td><td>Prism Central (host/IP); asked for if missing</td></tr><tr><td>`--user USER`</td><td>User; else `NTNX_USER`, else asked for</td></tr><tr><td>`-k`, `--insecure`</td><td>Do not verify the TLS certificate (self-signed)</td></tr><tr><td>`--csv FILE`</td><td>Also save the list as CSV</td></tr><tr><td>`--list-only`</td><td>Only list, never offer to delete</td></tr><tr><td>`-h`, `--help`</td><td>Show the built-in help</td></tr></tbody></table>

#### Password

Prompted (no echo). Never passed as argument, never stored. Alternatively set `NTNX_PASSWORD` (and `NTNX_USER`).

#### Mark and delete

After the list the script asks *Mark subnets for deletion? \[y/N\]*. Then:

<table id="bkmrk-keyaction-up-%2F-downm"><thead><tr><th>Key</th><th>Action</th></tr></thead><tbody><tr><td>Up / Down</td><td>Move</td></tr><tr><td>Space</td><td>Mark / unmark and move on</td></tr><tr><td>`a`</td><td>Mark all / none</td></tr><tr><td>Enter</td><td>Continue with the marked subnets</td></tr><tr><td>`q`</td><td>Quit without deleting</td></tr></tbody></table>

The marked subnets are shown again and need a final *Delete these N subnet(s) now? \[y/N\]* (default: no). Right before deleting the VMs are read again; a subnet that got a VM in the meantime is skipped. Subnets are deleted one by one, each with OK / FAIL.

#### Notes

- A VM counts whether it is powered on or off. Subnets are matched by UUID, so identical names on different clusters are not mixed up.
- Other consumers of a subnet (Files, Karbon, templates) are not checked. Prism may refuse such a delete - it shows up as FAIL.
- Without a terminal (pipe) or with `--list-only` the script only lists; nothing is deleted.