Delete downstream pipeline... but in python?

Hello,
is there any way to cleanly do the same action as ‘delete downstream pipeline’ but from python itself?

what i came up from some sketchy coding, is a function like this, but would like any inputs (to improve it) or if there is any cleaner solution:

def removeFilter(filter='',removeOnlyChildren=False):
    if filter is None or filter=='':
        filter = GetActiveSource()
    if isinstance(filter, str):
        filter = FindSource(filter)
    if not isinstance(filter,list):
        filters=[filter]
    else:
        filters=filter
    if removeOnlyChildren:
        fToKeep=[f for f in filters]
    else:
        fToKeep=[]
    # getting all sources that have as inputs the filter/filters given in filter function input
    # it will loop to get the sources that are input of input of input.... so one can remove all sublevels
    while True:
        added=False
        for proxy in GetSources().values():
            if proxy in filters:
                continue
            try:
                inputs=proxy.Input
                if not isinstance(inputs,servermanager.InputProperty):
                    inputs=[inputs]
                else:
                    inputs=[i for i in proxy.Input]
            except:
                continue
            if any(f in filters for f in inputs):
                filters.append(proxy)
                added=True
        if not added:
            break
    # ordering the filters to be deleted, this is done to remove any warning while removing sources that have children sources
    while filters:
        numberOfInputs=[0 for f in filters]
        for nF,proxy in enumerate(filters):
            try:
                inputs=proxy.Input
                if not isinstance(inputs,list):
                    inputs=[inputs]
            except:
                continue
            for input in inputs:
                if input in filters:
                    numberOfInputs[filters.index(input)]+=1
        cleannedFilters=[]
        for nF,f in enumerate(filters):
            if f in fToKeep:
                continue
            if numberOfInputs[nF]==0:
                Delete(f)
                del f
            else:
                cleannedFilters.append(f)
        gc.collect()
        filters=cleannedFilters
        if all([f in fToKeep for f in filters]):
            break
    return

while it is not clean (specially with the inputs, i did not find a cleanner way to deal between proxy.Input where the filter has a single input, and returns the proxy of the input itself and the proxy that returns a list that is not a list but a servermanager.InputProperty. from my testings it works but curious if there is any better way.