'Cleanup database after stack has completed/failed.
1. Delete the resources from DB.
2. If the stack failed, update the current_traversal to empty string
so that the resource workers bail out.
3. Delete previous raw template if stack completes successfully.
4. Deletes all sync points. They are no longer needed after stack
has completed/failed.
5. Delete the stack if the action is DELETE.'

def purge_db(self):

    resource_objects.Resource.purge_deleted(self.context, self.id)
    exp_trvsl = self.current_traversal
    if (self.status == self.FAILED):
        self.current_traversal = ''
    prev_tmpl_id = None
    if ((self.prev_raw_template_id is not None) and (self.status != self.FAILED)):
        prev_tmpl_id = self.prev_raw_template_id
        self.prev_raw_template_id = None
    stack_id = self.store(exp_trvsl=exp_trvsl)
    if (stack_id is None):
        LOG.warning('Failed    to    store    stack    %(name)s    with    traversal    ID    %(trvsl_id)s,    aborting    stack    purge', {'name': self.name, 'trvsl_id': self.current_traversal})
        return
    if (prev_tmpl_id is not None):
        raw_template_object.RawTemplate.delete(self.context, prev_tmpl_id)
    sync_point.delete_all(self.context, self.id, exp_trvsl)
    if ((self.action, self.status) == (self.DELETE, self.COMPLETE)):
        if (not self.owner_id):
            (status, reason) = self._delete_credentials(self.status, self.status_reason, False)
            if (status == self.FAILED):
                self.state_set(self.action, status, reason)
                return
        try:
            stack_object.Stack.delete(self.context, self.id)
        except exception.NotFound:
            pass
