'Reject (delete) all records in this resource'

def reject(self, cascade=False):

    db = current.db
    s3db = current.s3db
    define_resource = s3db.resource
    DELETED = current.xml.DELETED
    INTEGRITY_ERROR = current.ERROR.INTEGRITY_ERROR
    tablename = self.tablename
    table = self.table
    pkey = table._id.name
    get_config = s3db.get_config
    ondelete = get_config(tablename, 'ondelete')
    onreject = get_config(tablename, 'onreject')
    ondelete_cascade = get_config(tablename, 'ondelete_cascade')
    if ('uuid' in table.fields):
        rows = self.select([table._id.name, 'uuid'], as_rows=True)
    else:
        rows = self.select([table._id.name], as_rows=True)
    if (not rows):
        return True
    delete_super = s3db.delete_super
    if (DELETED in table):
        references = table._referenced_by
        for row in rows:
            error = self.error
            self.error = None
            if ondelete_cascade:
                callback(ondelete_cascade, row, tablename=tablename)
            for ref in references:
                try:
                    (tn, fn) = (ref.tablename, ref.name)
                except:
                    (tn, fn) = ref
                rtable = db[tn]
                rfield = rtable[fn]
                query = (rfield == row[pkey])
                if (rfield.ondelete in ('CASCADE', 'RESTRICT')):
                    rresource = define_resource(tn, filter=query, unapproved=True)
                    rresource.reject(cascade=True)
                    if rresource.error:
                        break
                elif (rfield.ondelete == 'SET    NULL'):
                    try:
                        db(query).update(**{fn: None})
                    except:
                        self.error = INTEGRITY_ERROR
                        break
                elif (rfield.ondelete == 'SET    DEFAULT'):
                    try:
                        db(query).update(**{fn: rfield.default})
                    except:
                        self.error = INTEGRITY_ERROR
                        break
            if ((not self.error) and (not delete_super(table, row))):
                self.error = INTEGRITY_ERROR
            if self.error:
                db.rollback()
                raise RuntimeError(('Reject    failed    for    %s.%s' % (tablename, row[table._id])))
            else:
                self.error = error
                error = None
                if onreject:
                    callback(onreject, row, tablename=tablename)
                fields = dict(deleted=True)
                if ('deleted_fk' in table):
                    record = table[row[pkey]]
                    fk = {}
                    for f in table.fields:
                        if ((record[f] is not None) and s3_has_foreign_key(table[f])):
                            fk[f] = record[f]
                            fields[f] = None
                        else:
                            continue
                    if fk:
                        fields.update(deleted_fk=json.dumps(fk))
                db((table._id == row[pkey])).update(**fields)
                if (s3_get_last_record_id(tablename) == row[pkey]):
                    s3_remove_last_record_id(tablename)
                if ondelete:
                    callback(ondelete, row, tablename=tablename)
    else:
        for row in rows:
            if ondelete_cascade:
                callback(ondelete_cascade, row, tablename=tablename)
            if onreject:
                callback(onreject, row, tablename=tablename)
            try:
                del table[row[pkey]]
            except:
                self.error = INTEGRITY_ERROR
                db.rollback()
                raise
            else:
                if (s3_get_last_record_id(tablename) == row[pkey]):
                    s3_remove_last_record_id(tablename)
                delete_super(table, row)
                if ondelete:
                    callback(ondelete, row, tablename=tablename)
    return True
