'This cleans up the script database of all non-persistent
scripts. It is called every time the server restarts.
Args:
obj (Object, optional): Only remove non-persistent scripts
assigned to this object.'

def remove_non_persistent(self, obj=None):

    if obj:
        to_stop = self.filter(db_obj=obj, db_persistent=False, db_is_active=True)
        to_delete = self.filter(db_obj=obj, db_persistent=False, db_is_active=False)
    else:
        to_stop = self.filter(db_persistent=False, db_is_active=True)
        to_delete = self.filter(db_persistent=False, db_is_active=False)
    nr_deleted = (to_stop.count() + to_delete.count())
    for script in to_stop:
        script.stop()
    for script in to_delete:
        script.delete()
    return nr_deleted
