Skip to content

Bulk delete unit types

Mikkel Rask edited this page Nov 27, 2023 · 3 revisions

Sometimes you just end up with 70 pages worth of units that you shouldn't have, and basicly only the first 12 created ones were actually needed. As you might have gotten already this script was made, for a very specific use case, but wanted to share it either way!


With the bulk-delete-unit-types.py tool, we are able to easily delete those with a ID higher (or lower with a little change!) that what we have set in the script (default set as 12, in the code, due to it's origin!)

API Key

As always make sure you fill your API key in the .env that allow us to perform actions on an agreement without the use of login credentials.

This script does not take a CSV file to execute, but rather just makes a request and gets back a response of the 20 first unit types with the highest value, and if their number is above min_unit_id, that unit type gets obliterated!

So it's extra important to be careful since we're talking deletion in this one! Please do make sure that the details are correct and deleting ALL unit types with a number above a specific valus is what wish for - this cannot be undone, and will work on all agreements no matter how many unit types they have, or what their ID is!

Set the min_unit_id

So like mentioned this script is currently set up to request a delete for all unit's with number higher than min_unit_id. The value of this, is set in the fourth line of the file, where it's defaulted to 12. Line 4 - min_unit_id

Do note that the operator used in this action is just the > greater than sign - on line 14 you are able to change this to >= (greater than or equal to), < (smaller than), <= (smaller than or equal to) or to match the number specifically with =.

The code almost speaks for itself in the screenshot If number is larger than other number, delete that guy: Notice how its basicly like reading "if the unit number is greater than 12"

Execute the script

Finally you are able to simply execute the script, and let Python do all the hard work!

py bulk-delete-unit-types.py >> unit-delete.log

from your Powershell Terminal, and the tool will chuck away. The >> unit-delete.log I added at the end, is mandatory and does block the output in the actual termial, but leaves you with a new file: unit-delete.log which will verify all actions, and errors. Great in situations where it's left unattended, or has MANY calls to go through!

I am unsure how long this would have taken through the UI, but deleting all ~1300 unit types (from our scenario) manually would have been a boring day at work, thats for sure.