# Cleanup Policies API

These REST API endpoints are used to manage cleanup policies to Nexus Repository. The user needs access to read and edit the `nexus:settings` permissions.

Assigning cleanup policies will require the use of the [Repositories API](https://help.sonatype.com/en/repositories-api.html "Repositories API").

Only available in Sonatype Nexus RepositoryTM Pro. Interested in a free trial? [Start here](/content/products/repository-pro/trial/index.html).

## [Get Cleanup Policies](https://help.sonatype.com/en/cleanup-policies-api.html#
get-cleanup-policies_body)

Request returns a list of cleanup policies. The user requires the following permissions: (`nexus:settings.read`)

```
GET /service/rest/v1/cleanup-policies
```

Example

```
curl -u admin:admin123 -X GET http://localhost:8081/service/rest/v1/cleanup-policies
```

## [Get Cleanup Policy by Policy Name](https://help.sonatype.com/en/cleanup-policies-api.html#
get-cleanup-policy-by-policy-name_body)

Required permissions (`nexus:settings.read`)

```
GET /service/rest/v1/cleanup-policies/{name}
```

- **`name`** \- User created name for the cleanup policy

Example

```
curl -u admin:admin123 -X GET http://localhost:8081/service/rest/v1/cleanup-policies/test-cleanup
```

See the _GET Cleanup Policy_ example for the response.

## [Create Cleanup Policy](https://help.sonatype.com/en/cleanup-policies-api.html#
create-cleanup-policy_body)

The user requires the following permissions: (`nexus:settings.edit`) The request to add a new cleanup policy is submitted in the body of a POST request. The endpoint will return a 201 when successful and a 409 when the name already exists.

```
POST /service/rest/v1/cleanup-policies
```

```
{
  "notes": "string",
  "criteriaLastBlobUpdated": 0,
  "criteriaLastDownloaded": 0,
  "criteriaReleaseType": "RELEASES",
  "criteriaAssetRegex": "string",
  "retain": null,
  "name": "policy-name",
  "format": "string"
}
```

The following parameters may be included in the body of the request. Any that are not included as criteria in the request are set to `null`. Some parameters are not accepted based on the supplied format.

See [Cleanup Policies](https://help.sonatype.com/en/cleanup-policies.html "Cleanup Policies") for details on which fields should be provided for a given format.

- `notes` \- any details on the specific cleanup policy.

- `criteriaLastBlobUpdated` \- the age of the component in days.

- `criteriaLastDownloaded` \- the last time the component had been downloaded in days.

- `criteriaReleaseType` \- When needed, this is either _PRELEASE_ or _RELEASE_.

- `criteriaAssetRegex` \- a regex string to filter for specific asset paths.

- `retain` \- number of versions to keep. Only available for Docker and Maven release repositories on PostgreSQL deployments.

- `name` \- the name of the policy needs to be unique and cannot be edited once set. Only letters, digits, underscores(_), hyphens(-), and dots(.) are allowed and may not start with underscore or dot.

- `format` \- the target format for the cleanup policy. Some formats have various capabilities and requirements. Note that you cannot currently specify all formats.

```
["apt", "bower", "cocoapods", "conan", "conda", "docker", "gitlfs", "go", "helm", "maven2", "npm", "nuget", "p2", "pypi", "r", "raw", "rubygems", "yum"]
```

Example Request

```
curl -u admin:admin123 -H 'accept:application/json' -H 'Content-Type: application/json' -d '{"name":"test-cleanup","format":"maven2", "criteriaLastDownloaded":10}' -X POST http://localhost:8081/service/rest/v1/cleanup-policies
```

### Retain Select Versions Cleanup Policy

Only available for select release repositories on PostgreSQL deployments. To use the `retain` parameter, you must also include at least one of the other release type criteria as well.

**Example Body for Retain Select Maven Component Versions**

```
{
  "criteriaLastDownloaded": 60,
  "criteriaReleaseType": "RELEASES",
  "retain": 3,
  "name": "maven-cleanup-policy",
  "format": "maven2"
}
```

The sorting order to retain latest versions is format specific. See [Cleanup Policies for Retain Select Versions](https://help.sonatype.com/en/cleanup-policies.html#retain-select-versions "Retain Select Versions") for more details.

**Note**

You may see the below message when attempting to use the `retain` property without PostgreSQL. This is due to the PostgreSQL requirement for using this functionality.

"_The current data source is not supported by the exclusion criteria_"

See the topic [Cleanup Policies for Retain Select Versions](https://help.sonatype.com/en/cleanup-policies.html#retain-select-versions "Retain Select Versions") for details.

## [Update Cleanup Policy](https://help.sonatype.com/en/cleanup-policies-api.html#
update-cleanup-policy_body)

Modify existing cleanup policies. The user requires the following permissions: (`nexus:settings.edit`). Use the policy name as a path parameter and inside the body object. The request returns a 204 on a successful update or 404 when the policy name is not found.

```
PUT /service/rest/v1/cleanup-policies/{name}
```

See the Create Cleanup Policy for body parameters. The policy name may not be modified.

## [Delete Cleanup Policy](https://help.sonatype.com/en/cleanup-policies-api.html#
delete-cleanup-policy_body)

Remove Cleanup Policy from Nexus Repository. The user requires the following permissions: (`nexus:settings.edit`). Returns a 204 response when the cleanup policy is successfully deleted, and a 404 when the policy name is not found.

```
DELETE /service/rest/v1/cleanup-policies/{name}
```

Example

```
curl -u admin:admin123 -X DELETE http://localhost:8081/service/rest/v1/cleanup-policies/test-cleanup
```

## Search results

No results found
