protected function cleanDuplicates($data, $service, $resource)
    {
        $cleaned = [];
        if (!empty($data)) {
            $rSeg = explode('/', $resource);
            $api = $service . '/' . $resource;

            switch ($api) {
                case 'system/admin':
                case 'system/user':
                    $key = 'email';
                    break;
                case 'system/cors':
                    $key = 'path';
                    break;
                case $service . '/_table/' . array_get($rSeg, 1);
                    $key = 'id';
                    break;
                default:
                    $key = 'name';
            }

            foreach ($data as $rec) {
                if (isset($rec[$key])) {
                    $value = array_get($rec, $key);
                    if (!$this->isDuplicate($service, $resource, array_get($rec, $key), $key)) {
                        $cleaned[] = $rec;
                    } elseif ($this->overwriteExisting === true) {
                        try {
                            if (true === static::patchExisting($service, $resource, $rec, $key)) {
                                $this->overwrote = true;
                                $this->log(
                                    'notice',
                                    'Overwrote duplicate found for ' . $api . ' with ' . $key . ' ' . $value
                                );
                            }
                        } catch (RestException $e) {
                            $this->throwExceptions(
                                $e,
                                'An unexpected error occurred. ' .
                                'Could not overwrite an existing ' .
                                $api . ' resource with ' . $key . ' ' .
                                $value . '. It could be due to your existing resource being exactly ' .
                                'same as your overwriting resource. Try deleting your existing resource and re-import.'
                            );
                        }
                    } else {
                        $this->log(
                            'notice',
                            'Ignored duplicate found for ' .
                            $api .
                            ' with ' . $key .
                            ' ' . $value
                        );
                    }
                } else {
                    $cleaned[] = $rec;
                    $this->log(
                        'warning',
                        'Skipped duplicate check for ' .
                        $api .
                        ' by key/field ' . $key .
                        '. Key/Field is not set'
                    );
                }
            }
        }

        return $cleaned;
    }