# Manually clean up sqlth\_te table?

**URL:** <https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251>\
**Category:** Ignition\
**Created:** [July 15, 2024, 6:27pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251 "2024-07-15T18:27:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toby\_Varland](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/toby_varland/32/65055_2.png) [@Toby\_Varland](https://forum.inductiveautomation.com/u/Toby_Varland)\
**Post date:** [July 15, 2024, 6:27pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/1 "2024-07-15T18:27:24Z")

</div>

Somebody on my team set up several thousand tags and configured them for history when they should not have. I can easily identify the records in my PostgreSQL database, and the tags themselves have been deleted from Ignition. Is there any reason I should not manually clean up the records in my PostgreSQL database?

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [July 15, 2024, 6:43pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/2 "2024-07-15T18:43:54Z")

</div>

I've had to manually excise just portions of data before; wholesale deletion should be relatively easier.

Get familiar with the following schema reference, and probably good to take a backup before you start deleting anything:

> **[Ignition Database Table Reference | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/reference-pages/ignition-database-table-reference#tag-history)**
>
> Ignition has a lot of systems built-in that will query the database automatically without requiring you to build a query. These systems automatically create the necessary tables in the database, insert the relevant data, and even query it back out...

EDIT: some interesting considerations in case you plan to also remove the actual data and not just the sqlth\_te entries:

> [@History data table clean on tag deletion?](https://forum.inductiveautomation.com/t/history-data-table-clean-on-tag-deletion/35360):
>
> Does anyone have a procedure that they have developed to ‘clean up’ the sqlt\_data\_ tables if and when history configuration is dropped for a tag? We are going to either stop collecting on a significant number of tags or remove the tags entirely. I would like to remove the history table entries for those tags. We do not ‘prune’ our history per se by date that is to say we are keeping our history indefinitely. An example of an SQL query that takes the old tag name as a parameter and then drops the…

---

<div class="post-metadata">

**Author:** ![Toby\_Varland](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/toby_varland/32/65055_2.png) [@Toby\_Varland](https://forum.inductiveautomation.com/u/Toby_Varland)\
**Post date:** [July 15, 2024, 6:53pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/3 "2024-07-15T18:53:19Z")

</div>

I think I'm fine with just letting the data fall off when the partitions are pruned (I'm only keeping data a short time (30 days) and my partitions are for a single day).

Thanks for the info, Felipe\_CRM.

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [July 16, 2024, 3:36am UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/4 "2024-07-16T03:36:01Z")

</div>

Bear in mind the pruning will only delete the data in the sqlth\_data\_XXXX tables. But sadly sometimes even after tag deletion, there can remain entries in the sqlth\_te table.  
This is average as your users may still find those "orphaned" tags when they use the Power Chart historical tag browser.  
So you might still want to manually clean up the database if thats the case.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 16, 2024, 11:38am UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/5 "2024-07-16T11:38:05Z")

</div>

You can query your partitions for distinct tag IDs that have data, and put that on the left of a left join with `sqlth_te`, then filter for nulls on the left to find the tag IDs that have no data.

---

<div class="post-metadata">

**Author:** ![msteele](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/msteele/32/77889_2.png) [@msteele](https://forum.inductiveautomation.com/u/msteele)\
**Post date:** [July 16, 2024, 12:57pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/6 "2024-07-16T12:57:16Z")

</div>

This may be a use case for the Tag History Transfer Tool in the Ignition Exchange:

> **[Ignition Exchange](https://inductiveautomation.com/exchange/2441/overview)**
>
> Discover, share, and download community made Ignition resources in collaboration with your organization or the Ignition community.

---

<div class="post-metadata">

**Author:** ![Toby\_Varland](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/toby_varland/32/65055_2.png) [@Toby\_Varland](https://forum.inductiveautomation.com/u/Toby_Varland)\
**Post date:** [July 16, 2024, 1:54pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/7 "2024-07-16T13:54:35Z")

</div>

I appreciate all the suggestions. I ran this query against my database and Identified tags that don't have any history from the past 5 days:

```python
SELECT *
FROM "sqlth_te"
WHERE "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240716")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240715")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240714")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240713")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240712");

```

I know I'm okay with getting rid of all these rows from `sqlth_te`. Therefore, if I change `SELECT *` to `DELETE`, I believe I should accomplish what I want:

```python
DELETE
FROM "sqlth_te"
WHERE "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240716")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240715")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240714")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240713")
  AND "id" NOT IN (SELECT DISTINCT "tagid" FROM "sqlt_data_1_20240712");

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 16, 2024, 3:06pm UTC](https://forum.inductiveautomation.com/t/manually-clean-up-sqlth-te-table/93251/8 "2024-07-16T15:06:45Z")

</div>

Looks good, but take a good database backup first.
