SonarQube Auto cleanup/housekeeping

Hi
I am using SonarQube Community edition 9.9.5 LTA.

I want to delete before 3 years old sonarqube project/analysis report using sonarqube webapi.

is it possible to delete sonarqube project only the project which have more than 5 analysis report.

if yes than please let me know what process i have to follow.

Hey there.

You can read about bulk deleting projects which have not been analyzed since before a given date here.

You can’t get more specific when using POST api/projects/bulk_delete, such as how many analyses the project has had.

To do that, you would need to iterate through all the projects (GET api/projects/search) and check for how many analyses were performed (GET api/project_analyses/search), and then call POST api/projects/delete for each project meeting your criteria.

I don’t quite understand why you’d want to delete projects that are more active than others (for example, a project with only one analysis).

Hi Colin

Thanks for your quick response.

I have to do the cleanup of SonarQube DB (Postgress).

Hi Colin

Sorry for the miscommunication.

I want to delete the project which have more than 5 release and older than 3 years.

for each different release separate report is created with unique project key.

So is it possible to filter using application release no.

NOTE: - every release has unique version, how we can filter using SonarQube web api/script that application have more than 5 release.

release no.: - 01.00.00,01.00.01,01.00.02,02.00.01,02.00.02,02.00.03

So, when we execute the script release no 01.00.00 should be deleted as there is total 6 releases.

You can also iterate through GET api/project_analyses/search to see how many unique project versions are returned.

{
  "paging": {
    "pageIndex": 1,
    "pageSize": 500,
    "total": 718
  },
    {
      "key": "b5c7b47b-e23f-436c-a6dc-6d88d158ec76",
      "date": "2024-11-15T15:53:17+0000",
      "events": [
        {
          "key": "c25d6dbf-4b6c-4c53-b543-6a6d588ea205",
          "category": "VERSION",
          "name": "10.8-SNAPSHOT"
        }
      ],
      "projectVersion": "10.8",
      "buildString": "10.8.0.99053",
      "manualNewCodePeriodBaseline": false,
      "revision": "6d8d5eb76d448c5d9473cd6e738f0767115ce296",
      "detectedCI": "CirrusCI"
    },
   {
      "key": "2149b828-b399-4d41-bbcf-074b66686a68",
      "date": "2024-09-27T07:40:55+0000",
      "events": [
        {
          "key": "fa99b66b-4f12-4396-a783-54831269ff61",
          "category": "VERSION",
          "name": "10.7-SNAPSHOT"
        }
      ],
      "projectVersion": "10.7-SNAPSHOT",
      "buildString": "10.7",
      "manualNewCodePeriodBaseline": false,
      "revision": "eff0da1de6742dc9a4c780f3635eeb1ec746dcfe",
      "detectedCI": "CirrusCI"
    },
...

If you’re trying to delete specific analyses, you can use POST api/project_analyses/delete with the key value returned from GET api/project_analyses/search.

Hi Colin,

As i have checked on web api I am not able to find the GET api/project_analyses_search .

I am getting GET api/project_analyses/search . Using this web api i have to pass the project key parameter. So, it will return the single project/release data.

So, using groovy/shell script how we can determine which project key.

I have to compare the project using date and no. of release on that basis i have to delete the project using script only.

You’re right, I’m sorry, that was a typo.

Hi Colin

I have tried using GET api/project_analyses/search webapi.

I am getting list of all the project/release.

We have more than 1000 of project all the projects have 5 or more than 5 release with different name and different version.

Here how we can filter without using project name. To pass project name is not possible.

Please confirm if there is any option.

Hey @Niraj

I’m sorry, I don’t really understand your last comment. In order to see the most recent analyses of each project to determine whether or not there’s something to cleanup, you’ll have to iterate through each project. This is a hard requirement of the GET api/project_analyses/search Web API. :person_shrugging: