Book a Demo

Author Topic: How to delete an element through API  (Read 5345 times)

sravang

  • EA User
  • **
  • Posts: 33
  • Karma: +0/-0
    • View Profile
How to delete an element through API
« on: December 10, 2020, 10:42:35 pm »
I need to delete an element,what will be the best way to do it??

If I use method delete and pass element ID to it does it delete that element and also delete all relations for that element??

Thanks,

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to delete an element through API
« Reply #1 on: December 10, 2020, 10:50:24 pm »
Get the owning element or package
Loop the Elements collection of the owner
Use Collection.DeleteAt() to delete the element.

something like this (deletes all elements from a package)
Code: [Select]
dim j
for j = package.Elements.Count -1 to 0 step -1
package.Elements.DeleteAt j , false
next

All relations, tagged values, diagramobjects etc.. will automatically be deleted by EA.

Geert

sravang

  • EA User
  • **
  • Posts: 33
  • Karma: +0/-0
    • View Profile
Re: How to delete an element through API
« Reply #2 on: December 10, 2020, 10:59:45 pm »
Above mentioned one deletes all the element of the package but I need to delete element of only particular ID and make sure all relations of that element are also deleted.

I mean is there a way I can pass element ID and delete that element from the package??

Thanks,

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to delete an element through API
« Reply #3 on: December 10, 2020, 11:10:09 pm »
Yeah, simply test the ID of each element in the loop to match your element ID and only delete if it matches.

Geert

sravang

  • EA User
  • **
  • Posts: 33
  • Karma: +0/-0
    • View Profile
Re: How to delete an element through API
« Reply #4 on: December 10, 2020, 11:11:35 pm »
Yes got it.

Thanks,

Slávek Rydval

  • EA User
  • **
  • Posts: 40
  • Karma: +2/-0
    • View Profile
    • homepage
Re: How to delete an element through API
« Reply #5 on: December 20, 2020, 10:33:15 am »
Be aware of the parent of the deleted element. If it is greater than zero then the element is not listed in Package.Elements but Parent Element.Elements. Thus this code (in C#) is more robust.

Code: [Select]
        public static void DeleteElement(EA.Repository Repository, EA.Element Element)
        {
            EA.Collection elements;

            if (Element.ParentID > 0)
                elements = Repository.GetElementByID(Element.ParentID).Elements;
            else
                elements = Repository.GetPackageByID(Element.PackageID).Elements;

            for (short i = (short)(elements.Count-1); i >= 0; i--)
            {
                if (((EA.Element)elements.GetAt(i)).ElementID == Element.ElementID)
                {
                    elements.DeleteAt(i, false);
                    return;
                }
            }
        }