---
title: "Automate repository cleanup based on last download date"
description: "We’ve added a last_downloaded field to packages in Cloudsmith, extending search-based package retention rules to enable cleanup of unused packages.  This feature allows you to create retention rules that automatically clean up packages in your repository based on usage, rather than just age or count, ensuring you retain only actively used packages."
canonical_url: "https://cloudsmith.com/changelog/automate-repository-cleanup-based-on-last-download-date"
last_updated: "2025-11-11T18:13:46.302Z"
---
# Automate repository cleanup based on last download date

We’ve added a `last_downloaded` field to packages in Cloudsmith, extending [search-based package retention rules]() to enable cleanup of unused packages.

This feature allows you to create retention rules that automatically clean up packages in your repository based on usage, rather than just age or count, ensuring you retain only actively used packages.

The new `last_downloaded` field is available for use in package queries when setting up or modifying retention rules via the Cloudsmith UI, API and Terraform provider. This field is also exposed in the API and UI, and can be used for general package search filtering.

## Example query

If you want to set up a retention rule that automatically deletes packages older than a year, you can include the following package filter in your retention rule:

```json
{
  "_key": "962e53399fda",
  "_type": "code",
  "code": "last_downloaded:<\"now - 366 days\"",
  "filename": null,
  "language": null,
  "markDefs": null
}
```

```json
{
  "_key": "d5adcea19035",
  "_type": "image",
  "alt": "Package filter query in Cloudsmith's retention rules feature",
  "asset": {
    "_createdAt": "2025-11-11T18:16:02Z",
    "_id": "image-4184eb41fc7bb438b5ee2c6d00876317ff97681a-2732x2048-png",
    "_rev": "afAHc2pwQE7DTHjhjOLGxV",
    "_type": "sanity.imageAsset",
    "_updatedAt": "2025-11-11T18:16:02Z",
    "assetId": "4184eb41fc7bb438b5ee2c6d00876317ff97681a",
    "extension": "png",
    "metadata": {
      "_type": "sanity.imageMetadata",
      "blurHash": "V8SZ2;xu~U^+s:_2IoRjRkRjMvWBkWbINH~pj?IpRjt6",
      "dimensions": {
        "_type": "sanity.imageDimensions",
        "aspectRatio": 1.333984375,
        "height": 2048,
        "width": 2732
      },
      "hasAlpha": true,
      "isOpaque": true,
      "lqip": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAByUlEQVR4nI2T624aMRBGef/n688GBaWBAGFvvnvGPpWdBdFA1a50ZGlWPvvtaGYTU6ZRSun4EFmM62cWJYus6A3VQuX5szEusFhHShlVvQmN81gfsT6sRNxKzJlSnys3i/WM89JFotrTOh9wIX6xStp7HxM+RlIW6t+ExgVmY4kpIVpwITOawLAELrPnMgcuS2AyCeMFEwQXBdH69Lc31x62vmStDCazO3m2H47th2V7+GJ3ChxG4Tgr5znjYqFU+B5003rR4re6lsrilcOYeL9E9kPiMMTOx5g5L8rFFAar+FTRQpdeafLNvb0VQ65MXhidYmPtFzv5Cv0MUkkKeUUU9LmwMDphctovR3mkfdTncpOmVfogrKtwsMLkdU3CA63uUiH+lzAVRivMQZ/KWqomc1H/LSxdqCwu9dGIok8JSfBtOqT8Q1gKoW+KvduW66YErLvicd4TYiLdSduU3AkrtSjWOg7nmd1x4e1keDtZfp3/ZP/p+ZxCl7at6UKpZCmPQmct78eJl/3My35hezC8Hu2N3cnxdg6cr0LJJK0YnzmPy3ehkLxhngYuw8AwjkzzjLEG6+wdDu8sMViSCEGU07Dw4+crvwGF8pPhPDDwWQAAAABJRU5ErkJggg==",
      "palette": {
        "_type": "sanity.imagePalette",
        "darkMuted": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#4d5860",
          "foreground": "#fff",
          "population": 0.01,
          "title": "#fff"
        },
        "darkVibrant": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#1a2c3a",
          "foreground": "#fff",
          "population": 0.08,
          "title": "#fff"
        },
        "dominant": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#aabdd1",
          "foreground": "#000",
          "population": 0.27,
          "title": "#fff"
        },
        "lightMuted": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#aabdd1",
          "foreground": "#000",
          "population": 0.27,
          "title": "#fff"
        },
        "lightVibrant": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#a0ccfc",
          "foreground": "#000",
          "population": 0.04,
          "title": "#fff"
        },
        "muted": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#667890",
          "foreground": "#fff",
          "population": 0.04,
          "title": "#fff"
        },
        "vibrant": {
          "_type": "sanity.imagePaletteSwatch",
          "background": "#6080b8",
          "foreground": "#fff",
          "population": 0,
          "title": "#fff"
        }
      }
    },
    "mimeType": "image/png",
    "originalFilename": "retentionrules_lastdownloaded.png",
    "path": "images/rafvlnhi/production/4184eb41fc7bb438b5ee2c6d00876317ff97681a-2732x2048.png",
    "sha1hash": "4184eb41fc7bb438b5ee2c6d00876317ff97681a",
    "size": 406326,
    "uploadId": "lfLax9BysTJOAfFlGmXKqJTP6EavUV3p",
    "url": "https://cdn.sanity.io/images/rafvlnhi/production/4184eb41fc7bb438b5ee2c6d00876317ff97681a-2732x2048.png"
  },
  "link": {
    "_type": "link",
    "href": null,
    "openInNewTab": false
  },
  "markDefs": null
}
```

```json
{
  "_key": "a47c60c598aa",
  "_type": "callout",
  "content": [
    {
      "_key": "e9bc978699d7",
      "_type": "block",
      "children": [
        {
          "_key": "f51503a4dc4e",
          "_type": "span",
          "marks": [],
          "text": "This functionality is not currently available for Docker images, but Docker support is planned for an upcoming release. Additionally, if a package is only proxied through Cloudsmith and never downloaded from the cache afterward, its total downloads will be 0, and the "
        },
        {
          "_key": "b70510d475d9",
          "_type": "span",
          "marks": [
            "code"
          ],
          "text": "last_downloaded"
        },
        {
          "_key": "4247fd7f4c45",
          "_type": "span",
          "marks": [],
          "text": " date will be null."
        }
      ],
      "markDefs": [],
      "style": "normal"
    }
  ],
  "markDefs": null,
  "variant": "info"
}
```
