# PersistentRecord not deleting?

**URL:** <https://forum.inductiveautomation.com/t/persistentrecord-not-deleting/76006>\
**Category:** Module Development\
**Tags:** ignition81\
**Created:** [July 6, 2023, 3:21pm UTC](https://forum.inductiveautomation.com/t/persistentrecord-not-deleting/76006 "2023-07-06T15:21:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_McIlvenna](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/robert_mcilvenna/32/80238_2.png) [@Robert\_McIlvenna](https://forum.inductiveautomation.com/u/Robert_McIlvenna)\
**Post date:** [July 6, 2023, 3:21pm UTC](https://forum.inductiveautomation.com/t/persistentrecord-not-deleting/76006/1 "2023-07-06T15:21:39Z")

</div>

I'm having trouble deleting a persistent record.  
I use find() to get one of them, and call  
theRecord.deleteRecord();  
However, I can still query for the record, its still in the database.

boolean isDelete = theRecord.isDeleted();  
boolean isDirty = theRecord.isDirty();

However, isDelete is true, so it did something. But since isDirty is also true, it hasn't been flushed to the database? How often is this done, or is this something I have to call myself? I've even restarted the gateway and it is still there?

Here's the relevant part of the doc:

> public void deleteRecord()  
> Sets a flag to delete this record when the transaction is commited. The record is not removed from the transaction cache. Any future findOrCreate will thus return the deleted record **but its fields may not be referenced**. (isDeleted can be used to determine that the record has been deleted.)  
> The record is only deleted from the database when the transaction is committed or is flushed. Thus a transaction that nulls out references to a parent record and then deletes the parent record will not cause a referential integrity violation because the update order is preserved so that the updates will (normally) be performed before the deletes.

And what's weirder, is that I can still reference the fields? Using isDeleted should work for the time being, but this behavior seems odd, and I'd like to be able to completely remove these entries permanently.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 6, 2023, 3:40pm UTC](https://forum.inductiveautomation.com/t/persistentrecord-not-deleting/76006/2 "2023-07-06T15:40:02Z")

</div>

You should be opening a session with `PersistenceInterface.getSession()` before you do anything. Wrap that in a try-with-resources. When you're ready to delete a record:

1. Construct a `new KeyValue(yourRecord)`
2. Delete the record from the instance.
3. Tell the rest of the system you deleted your record, so any RecordListeners get notified:  
`context.getPersistenceInterface().notifyRecordDeleted(yourRecordMeta, yourKeyValue);`
4. Call `commit()` on the session object (within your try-with-resources scope).
