# Purge objects

**fylr** cannot currently actually purge objects from database. All objects are stored in historic versions, also deleted objects are kept in the database with a flag deleted. These objects can be linked to files. Those files are not deleted by the file janitor as **fylr** considers them to be *in use.*

This article describes a method to purge objects using SQL access to the fylr database. After the purge of objects, the file janitor will acknowledge the deleted file links and eventually remote the files and free disk space.

{% hint style="info" %}
Make sure to have a **backup before** using this procedure. This is a very dangerous tool and can easily lead to data loss. The SQL here is provided as is, with **no warranty and responsibility.**

**Use at your own risk!**
{% endhint %}

The simple form of the deleting command looks like this:

<pre class="language-sql"><code class="lang-sql"><strong>BEGIN;
</strong><strong>DELETE FROM "object" WHERE "system_object_id" IN (
</strong>  SELECT "system_object_id" 
    FROM "object" WHERE "latest_version" AND "deleted_at" IS NOT NULL
);
ROLLBACK;
</code></pre>

In case the above throws a foreign key violation, you need to remove links to existing objects first.

```sql
BEGIN;
-- DELETE links to objects to be deleted
DELETE FROM "value" WHERE "linked_system_object_id" IN (
   SELECT "system_object_id" 
     FROM "object" WHERE "latest_version" AND "deleted_at" IS NOT NULL
);
-- DELETE objects in all versions where the latest object is deleted
DELETE FROM "object" WHERE "system_object_id" IN (
  SELECT "system_object_id" 
    FROM "object" WHERE "latest_version" AND "deleted_at" IS NOT NULL
);
ROLLBACK;
```

You need to replace the `ROLLBACK` with `COMMIT` to really put this into effect.

The `WHERE` clauses can be changed here, to limit the delete run to certain pools (`pool_id`) or object types (`table_api_id`).

After you are done make sure to do a re-index using `/inspect/system`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fylr.io/for-system-administrators/symptom-and-solution/purge-objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
