Delete objects within APIC

APIC models the entire fabric with a tree of managed objects (MO). Once we have the managed object the deletion is very simple:

    
from cobra.mit.access import MoDirectory
from cobra.mit.session import LoginSession
from cobra.mit.request import ConfigRequest

session = LoginSession(url, user, password)
moDir = MoDirectory(session)
moDir.login()

def delete_mo(mo_object):
        if mo_object is not None:
            mo_object.delete()
            configReq = ConfigRequest()
            configReq.addMo(mo_object)
            moDir.commit(configReq)

sp_testing = moDir.lookupByDn('uni/infra/nprof-sp-testing')
delete_mo(sp_testing)
    

The previous example shows how you can delete a managed object using its distinguished name (a switch profile with name sp-testing). Switch profiles are objects made from 'infraNodeP' class, so you could also use the class name instead of the distinguished name. Be careful though! The class query returns ALL objects from that class, where the distinguished name query always return the only object associated with that distinguished name.

    
from cobra.mit.access import MoDirectory, ClassQuery
from cobra.mit.session import LoginSession
from cobra.mit.request import ConfigRequest


session = LoginSession('https://dp6-apic1.cisco.com', 'admin', 'cisco.123')
moDir = MoDirectory(session)
moDir.login()

def delete_mo(mo_object):
        if mo_object is not None:
            mo_object.delete()
            configReq = ConfigRequest()
            configReq.addMo(mo_object)
            moDir.commit(configReq)

class_query = ClassQuery('infraNodeP')
sp_list = moDir.query(class_query)
for mo in sp_list:
    if mo.name == 'sp-testing':
        delete_mo(mo)