# Drone database periodical maintenance

**URL:** <https://drone.discourse.group/t/drone-database-periodical-maintenance/9633>\
**Category:** Drone Support\
**Created:** [November 3, 2020, 1:51pm UTC](https://drone.discourse.group/t/drone-database-periodical-maintenance/9633 "2020-11-03T13:51:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanona](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/zanona/32/2624_2.png) [@zanona](https://drone.discourse.group/u/zanona)\
**Post date:** [November 3, 2020, 1:51pm UTC](https://drone.discourse.group/t/drone-database-periodical-maintenance/9633/1 "2020-11-03T13:51:14Z")

</div>

What would be the advised way of performing regular database maintenance every once in a while? _i.e: removing very old builds, removing legacy deployments which are not longer used, etc?_

---

<div class="post-metadata">

**Author:** ![csgitharness](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/csgitharness/32/904_2.png) [@csgitharness](https://drone.discourse.group/u/csgitharness)\
**Post date:** [November 5, 2020, 12:40pm UTC](https://drone.discourse.group/t/drone-database-periodical-maintenance/9633/2 "2020-11-05T12:40:35Z")

</div>

Hi there, Here is the sample query that might help

```auto
-- delete old logs
DELETE FROM logs WHERE log_job_id IN (
  SELECT proc_id FROM procs WHERE proc_build_id IN (
    SELECT build_id
    FROM repos, builds
    WHERE repo_id = build_repo_id 
    AND build_number < repo_counter - 20
  )
);

-- delete old procs
DELETE FROM procs WHERE proc_build_id IN (
  SELECT build_id
  FROM repos, builds
  WHERE repo_id = build_repo_id 
  AND build_number < repo_counter - 20
);

-- delete old builds
DELETE FROM builds WHERE build_id IN (
  SELECT build_id
  FROM repos, builds
  WHERE repo_id = build_repo_id 
  AND build_number < repo_counter - 20
);

```

Also check an issue already reported earlier with possible solutions: [https://github.com/drone/drone/issues/2440](https://github.com/drone/drone/issues/2440)

---

<div class="post-metadata">

**Author:** ![zanona](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/zanona/32/2624_2.png) [@zanona](https://drone.discourse.group/u/zanona)\
**Post date:** [November 10, 2020, 12:47pm UTC](https://drone.discourse.group/t/drone-database-periodical-maintenance/9633/3 "2020-11-10T12:47:25Z")

</div>

Thanks so much for the sample and also for the link! All I needed. 🙂
