By albert
I need to delete all droplets with the name xyz. However when I run:
doctl compute droplet delete xyz -f
However this only works if there is exactly one droplet with the name xyz.
If there are >1 droplets I get:
Error: There are 2 Droplets with the name "xyz"; please provide a specific Droplet ID. [1111111, 9999999]
If there are 0 droplets I get:
Error: Droplet with the name "xyz" could not be found.
The problem is that I don’t know how many droplets have the name xyz when my script is run.
I tried:
doctl compute droplet delete xyz -f --all
But the --all flag does not exist. What would you recommend if I want to delete all droplets (I want this to work if there are 0, 1, 2, … n droplets, not just if there is exactly one droplet)
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hello Albert,
Indeed, as the --all
flag doesn’t exist for the doctl compute droplet delete
command, you can achieve your goal by combining a few commands. You can list all Droplets, filter those with the name ‘xyz’, extract their IDs, and then delete each one. Here’s how you can do it in a shell script or command line:
doctl compute droplet list --format "ID,Name" --no-header | grep " xyz" | awk '{print $1}' | xargs -I {} doctl compute droplet delete {} -f
This command sequence does the following:
doctl compute droplet delete
to delete the Droplets without confirmation prompts.Note that this is a destructive action so make sure to test this script with caution, especially when using -f
to force deletion without confirmation. You can for example only run the first part to view the IDs that will be passed to the delete command:
doctl compute droplet list --format "ID,Name" --no-header | grep " xyz" | awk '{print $1}'
Regarding your suggestion to have a more straightforward way to handle this, I recommend submitting a feature request to the doctl
GitHub repository. You can propose the addition of an --all
flag or a similar feature that allows users to delete multiple droplets by name more seamlessly. Here’s the link to create a new issue: Submit a Feature Request to doctl.
If you have any more questions or need further assistance, feel free to ask!
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.