actions/delete-package-versions

repository·main·Indexed 19 days ago

https://github.com/actions/delete-package-versions

A GitHub Action to automate the deletion of package versions from GitHub Packages. It supports various package types including container, maven, npm, nuget, and rubygems. The action allows for managing retention policies by deleting specific version IDs, removing the oldest versions, keeping a minimum number of latest versions, or targeting pre-release and untagged versions using regex patterns for exclusion.

Tokens
3.2K
Snippets
9
Records
13
Agent score
64%

What's inside actions/delete-package-versions

  1. Understand valid input combinations for deletion

    main

    When configuring the action, you must use compatible input combinations. The following combinations are valid when used with the required parameters (owner, package-name, package-type, and token):

    • num-old-versions-to-delete (alone)
    • min-versions-to-keep (alone)
    • delete-only-pre-release-versions (alone)
    • ignore-versions (alone)
    • num-old-versions-to-delete + ignore-versions
    • min-versions-to-keep + ignore-versions
    • min-versions-to-keep + delete-only-pre-release-versions
    • delete-only-untagged-versions (alone)
    • min-versions-to-keep + delete-only-untagged-versions
  2. Authentication requirements for deleting packages

    main

    The token input is used to authenticate with GitHub Packages.

    • Default Behavior: Defaults to github.token.
    • When it is required: If the repository running the workflow does not have permission to delete the package.
      • For rubygems and maven: The repo has access if the package is hosted in the same repo as the workflow.
      • For container, npm, and nuget: The repo must be assigned the Admin role under Package Settings > Manage Actions Access.
    • Scope Requirements:
      • If package-version-ids is provided: The token only needs the delete packages scope.
      • If package-version-ids is NOT provided: The token needs both delete packages and read packages scopes.
  3. Use actions/delete-package-versions@v5

    main

    The actions/delete-package-versions action allows you to manage GitHub Packages by deleting specific versions, implementing retention policies, or cleaning up old/pre-release/untagged versions.

    Important Limitations:

    • The action will delete a maximum of 100 versions in a single run.
    • Some input parameters are mutually exclusive (e.g., you cannot use num-old-versions-to-delete and min-versions-to-keep together).
    - uses: actions/delete-package-versions@v5
      with:
        package-name: "your-package-name"
        package-type: "npm"
        owner: "your-org-or-user"
        token: ${{ secrets.GITHUB_TOKEN }}
  4. Delete the oldest X number of versions while ignoring specific versions

    main

    To delete a specific count of the oldest versions while protecting certain versions via regex, use num-old-versions-to-delete and ignore-versions.

    Note: If the oldest $X$ versions include versions that match the ignore-versions pattern, fewer than $X$ versions will actually be deleted.

    Required inputs: package-name, package-type, num-old-versions-to-delete, and ignore-versions.

    - uses: actions/delete-package-versions@v5
      with: 
        package-name: 'test-package'
        package-type: 'npm'
        num-old-versions-to-delete: 3
        ignore-versions: '^(0|[1-9]\d*)\.0\.0$'
  5. Delete all except Y latest versions while ignoring specific versions

    main

    To keep the latest $Y$ versions but protect specific versions (like major releases) from deletion, use the ignore-versions input with a regex pattern.

    Required inputs: package-name, package-type, min-versions-to-keep, and ignore-versions.

    - uses: actions/delete-package-versions@v5
      with: 
        package-name: 'test-package'
        package-type: 'npm'
        min-versions-to-keep: 3
        ignore-versions: '^(0|[1-9]\d*)\.0\.0$'
  6. Delete all pre-release versions except the latest Y versions

    main

    To delete all pre-release versions while keeping a specific number of the most recent pre-release versions, use delete-only-pre-release-versions: 'true' and min-versions-to-keep.

    Required inputs: package-name, package-type, min-versions-to-keep, and delete-only-pre-release-versions.

    - uses: actions/delete-package-versions@v5
      with: 
        package-name: 'test-package'
        package-type: 'npm'
        min-versions-to-keep: 10
        delete-only-pre-release-versions: "true"
  7. Delete all untagged container versions except the latest Y versions

    main

    To clean up untagged container images, use delete-only-untagged-versions: 'true'.

    Note: package-type must be set to container for this scenario.

    Required inputs: package-name, package-type, min-versions-to-keep, and delete-only-untagged-versions.

    - uses: actions/delete-package-versions@v5
      with: 
        package-name: 'test-package'
        package-type: 'container'
        min-versions-to-keep: 10
        delete-only-untagged-versions: 'true'
  8. Delete specific package versions by ID

    main

    To delete exact versions, provide their unique IDs to the package-version-ids input. This input accepts a single ID or a comma-separated list of IDs.

    IDs can be retrieved via the GitHub REST API.

    Required inputs: package-name, package-type, and package-version-ids.

    - uses: actions/delete-package-versions@v5
      with:
        package-version-ids: 'MDE0OlBhY2thZ2VWZXJzaW9uOTcyMDY3, MDE0OlBhY2thZ2VWZXJzaW9uOTcyMzQ5'
        package-name: 'test-package'
        package-type: 'npm'
  9. Configure the Delete Package Versions action

    main

    The delete-package-versions action allows you to delete specific package versions or prune old versions based on retention policies. You can configure it using the following input parameters in your GitHub Actions workflow:

    • package-version-ids: A single package version ID or a comma-separated list of IDs. Defaults to an empty string.
    • owner: The owner of the package. Defaults to the owner of the repository executing the workflow. If the package is hosted in a different organization, this is required.
    • package-name: The name of the package. Required.
    • package-type: The type of the package. Supported values: docker (v4 or older), container (v5 or newer), maven, npm, nuget, or rubygems. Required.
    • num-old-versions-to-delete: The number of old versions to delete.
    • min-versions-to-keep: The minimum number of versions to keep.
    • ignore-versions: A regular expression used to ignore specific versions.
    • delete-only-pre-release-versions: Set to true to only delete pre-release versions.
    • delete-only-untagged-versions: Set to true to only delete untagged versions.
    • token: The authentication token used to perform the deletions.
    - name: Delete Package Versions
      uses: actions/delete-package-versions@v5
      with:
        package-name: "my-package"
        package-type: "npm"
        owner: "my-org"
        package-version-ids: "1.0.0,1.0.1"
        token: ${{ secrets.GH_TOKEN }}
  10. Configure the delete-package-versions action inputs

    main

    The actions/delete-package-versions@v5 action uses several inputs to identify which packages and versions to remove.

    Core Inputs

    • package-name: (Required) The name of the package.
    • package-type: (Required) The type of the package. Supported values: docker (v4 or older), container (v5 or newer), maven, npm, nuget, or rubygems.
    • owner: The owner of the package. Defaults to the owner of the repository executing the workflow. Required if the package is hosted in a different organization than the executing workflow.
    • token: A GitHub token required if deleting from a package hosted in a different organization. The token needs delete:packages and read:packages scopes. It is recommended to use a GitHub Secret (e.g., ${{ secrets.GITHUB_PAT }}).

    Selection Inputs

    • package-version-ids: A single package version ID or a comma-separated list of IDs. IDs can be retrieved via the GitHub REST API.
    • min-versions-to-keep: The number of latest versions to preserve.
    • num-old-versions-to-delete: The number of oldest versions to delete.
    • ignore-versions: A regex pattern to ignore specific versions (e.g., using semver patterns).
    • delete-only-pre-release-versions: Set to `
  11. Configure delete-package-versions input parameters

    main

    Use the following inputs to control how package versions are deleted.

    Required Inputs

    • package-name: The name of the package.
    • package-type: The type of package. Supported values: docker (v4 or older), container (v5 or newer), maven, npm, nuget, or rubygems.
    • owner: The owner of the package. Defaults to the owner of the repo executing the workflow. Required if the package is hosted in a different organization than the workflow.
    • token: The token used to authenticate. Defaults to github.token. Required if the repository running the workflow does not have direct access to delete the package.

    Version Selection and Retention

    • package-version-ids: A single package version ID or a comma-separated list of IDs. Defaults to an empty string.
    • num-old-versions-to-delete: The number of old versions to delete starting from the oldest. Defaults to 1. (Mutually exclusive with min-versions-to-keep and delete-only-pre-release-versions).
    • min-versions-to-keep: The number of latest versions to keep. If set to 0, all deletable versions are deleted. This takes precedence over num-old-versions-to-delete.
    • ignore-versions: A regex for the version name to exclude from deletion. (Ignored if delete-only-pre-release-versions is true).

    Specialized Deletion

    • delete-only-pre-release-versions: If true, only pre-release versions are deleted. Can be used with min-versions-to-keep. (Mutually exclusive with num-old-versions-to-delete and ignore-versions).
    • delete-only-untagged-versions: If true, only untagged versions are deleted (for container packages only). Can be used with min-versions-to-keep. (Mutually exclusive with num-old-versions-to-delete).
  12. Delete a single package version with deletePackageVersion()

    main

    Use deletePackageVersion to delete a specific package version via the GitHub API. It returns an Observable<boolean> that resolves to true if the version was successfully deleted (HTTP 204).

    If the API call fails, the error message will include the specific error from GitHub or a fallback message indicating how many versions were successfully deleted prior to the failure.

    // Note: This function returns an RxJS Observable
    deletePackageVersion(
      packageVersionId: string, // The numeric ID of the package version
      owner: string,            // The owner of the package
      packageName: string,      // The name of the package
      packageType: string,      // The type of package (e.g., 'npm', 'maven', 'container', etc.)
      token: string             // GitHub personal access token
    ): Observable<boolean>