Add a new API to delete the swatch
There is a problem with global gradient swatches, described at https://illustrator.uservoice.com/forums/908050/suggestions/44461230
Illustrator applies changes to all copies of a gradient in a document when this gradient is changed via a script. It makes sense from a practical point of view, but is devastating for scripts in general.
We can’t duplicate gradients with a script properly, like we can do with any other swatch, because when it's done, the gradient swatch get forcibly added to Swatches and then corrupt the art which uses them when deleted... and it does not break this global link anyway.
At the moment we copy a gradient into the swatch, it will be associated with the first object. If we repaint the swatch to use it for another object, the first object will change its fill momentarily. Just like the video from the issue above shows — https://illustrator.uservoice.com/assets/235626175/linked-gradients-bug.mp4
So we 'instance' gradients instead of 'duplicating' them.
Creating a new gradient via script also creates a new swatch. Deleting either the new gradient or the new swatch also deletes the other. If the need is to get rid of the swatch but not the gradient, that doesn’t look to be possible via with the current APIs. Perhaps we need a new API to delete just the swatch and not the associated global (gradient, pattern etc.).
Having this will allow us to keep gradient changes on one object from affecting other objects. To duplicate, a new gradient will be created and a copy of all gradient properties (type, number of stops, stops) will be performed, via existing app.activeDocument.gradients.add()
Then we will be able to delete the swatch with a new API without crashing Illustrator.