[{"data":1,"prerenderedAt":7},["ShallowReactive",2],{"docs-source-storage\u002Fcleanup":3,"docs-tabs-source-storage\u002Fcleanup":6},{"content":4,"lastmod":5},"## Clear old versions\n\n`POST \u002Fv1\u002Fapps\u002F:id\u002Fsources\u002Fcleanup`\n\nDeletes old versions, keeping the `keepLatest` most recent ones. Versions with the `manual` or `published` tag are excluded from cleanup regardless of `keepLatest`. The operation is irreversible — deleted versions cannot be restored through the API.\n\n## Parameters\n\n| Parameter | Type | Required | Description |\n|----------|-----|:-----:|----------|\n| `id` (path) | UUID | yes | Application identifier. Get it via [`GET \u002Fv1\u002Fapps`](\u002Fdocs\u002Fapps\u002Flist). |\n\n## Request fields (body)\n\n| Field | Type | Required | Default | Description |\n|------|-----|:-----:|-----------|----------|\n| `keepLatest` | number | no | `5` | How many of the most recent versions to keep. A non-negative integer. `0` keeps only versions with the `manual` or `published` tag. |\n\nThe body can be omitted — the default value applies.\n\n## Examples\n\n### curl — personal key\n\n```bash\ncurl -X POST https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fapps\u002F\u003CAPP_ID>\u002Fsources\u002Fcleanup \\\n  -H \"X-Api-Key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{ \"keepLatest\": 3 }'\n```\n\n### curl — OAuth application\n\n```bash\ncurl -X POST https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fapps\u002F\u003CAPP_ID>\u002Fsources\u002Fcleanup \\\n  -H \"X-Api-Key: YOUR_APP_KEY\" \\\n  -H \"Authorization: Bearer USER_SESSION_TOKEN\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{ \"keepLatest\": 3 }'\n```\n\n### JavaScript — personal key\n\n```javascript\nconst res = await fetch(\n  `https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fapps\u002F${appId}\u002Fsources\u002Fcleanup`,\n  {\n    method: 'POST',\n    headers: {\n      'X-Api-Key': 'YOUR_API_KEY',\n      'Content-Type': 'application\u002Fjson',\n    },\n    body: JSON.stringify({ keepLatest: 3 }),\n  },\n)\nconst { data } = await res.json()\nconsole.log('Deleted versions:', data.deletedVersions.length)\n```\n\n### JavaScript — OAuth application\n\n```javascript\nconst res = await fetch(\n  `https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fapps\u002F${appId}\u002Fsources\u002Fcleanup`,\n  {\n    method: 'POST',\n    headers: {\n      'X-Api-Key': 'YOUR_APP_KEY',\n      'Authorization': 'Bearer USER_SESSION_TOKEN',\n      'Content-Type': 'application\u002Fjson',\n    },\n    body: JSON.stringify({ keepLatest: 3 }),\n  },\n)\nconst { data } = await res.json()\nconsole.log('Deleted versions:', data.deletedVersions.length)\n```\n\n## Response fields\n\n| Field | Type | Description |\n|------|-----|----------|\n| `success` | boolean | Always `true` on success. |\n| `data.deletedVersions` | string[] | Identifiers of the deleted versions. An empty array means the cleanup removed no versions. |\n\n## Response example\n\n`HTTP 200`:\n\n```json\n{\n  \"success\": true,\n  \"data\": {\n    \"deletedVersions\": [\"v2\", \"v1\"]\n  }\n}\n```\n\n## Error response example\n\n400 — `keepLatest` is not a non-negative integer:\n\n```json\n{\n  \"success\": false,\n  \"error\": {\n    \"code\": \"INVALID_KEEP_LATEST\",\n    \"message\": \"keepLatest must be a non-negative integer\"\n  }\n}\n```\n\n## Errors\n\n| HTTP | Code | Description |\n|------|-----|----------|\n| 400 | `INVALID_KEEP_LATEST` | The `keepLatest` value is not a non-negative integer. |\n| 403 | `SOURCE_APP_ID_MISMATCH` | The call was made with an authorization key `vibe_app_…` issued for a different application. Such a key can access only its own application's snapshots, even when both applications were created by the same author. |\n| 403 | `NOT_AUTHORIZED` | Only the application author, the application OAuth key, or a Bitrix24 account administrator can manage snapshots. |\n| 403 | `INFRA_FORBIDDEN_FOR_COWORK_KEY` | The call was made with a Cowork key — such a key works with data only and cannot perform write operations. To issue a key that can, see [Project key for deploy](\u002Fdocs\u002Fcowork\u002Fdeploy-key). |\n| 404 | `APP_NOT_FOUND` | The application does not exist, was deleted, or belongs to another portal. |\n\nFull list of common API errors — [Errors](\u002Fdocs\u002Ferrors).\n\n## See also\n\n- [Delete a version](\u002Fdocs\u002Fsource-storage\u002Fdelete)\n- [Source code storage](\u002Fdocs\u002Fsource-storage)\n- [Version lifetime and cleanup](\u002Fdocs\u002Fsource-storage\u002Fretention)\n- [Version tags and notes](\u002Fdocs\u002Fsource-storage\u002Fmetadata)\n- [List versions](\u002Fdocs\u002Fsource-storage\u002Fversions)\n- [Storage](\u002Fdocs\u002Fstorage)\n","2026-11-15",{},1795415506244]