diff --git a/cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py b/cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py index 006f2ed98..ce3a479e5 100644 --- a/cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py +++ b/cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py @@ -132,11 +132,17 @@ class SQLAlchemyAdapter(): # Don't delete local file unless this is the only reference to the file in the database if len(raw_data_location_entities) == 1: + # delete local file only if it's created by cognee from cognee.base_config import get_base_config config = get_base_config() + if config.data_root_directory in raw_data_location_entities[0].raw_data_location: - os.remove(raw_data_location_entities[0].raw_data_location) + if os.path.exists(raw_data_location_entities[0].raw_data_location): + os.remove(raw_data_location_entities[0].raw_data_location) + else: + # Report bug as file should exist + pass await session.execute(delete(Data).where(Data.id == data_id)) await session.commit() diff --git a/cognee/tests/test_pgvector.py b/cognee/tests/test_pgvector.py index 9da34c3d4..417904089 100644 --- a/cognee/tests/test_pgvector.py +++ b/cognee/tests/test_pgvector.py @@ -26,7 +26,8 @@ async def test_local_file_deletion(data_text, file_location): assert os.path.isfile(data.raw_data_location), f"Data location doesn't exist: {data.raw_data_location}" # Test deletion of data along with local files created by cognee await engine.delete_data_entity(data.id) - assert not os.path.exists(data.raw_data_location), f"Data location exists: {data.raw_data_location}" + assert not os.path.exists( + data.raw_data_location), f"Data location still exists after deletion: {data.raw_data_location}" async with engine.get_async_session() as session: # Get data entry from database based on file path