SQLAlchemy Easy Soft-Delete
Easily add soft-deletion to your SQLAlchemy Models and automatically filter out soft-deleted objects from your queries and relationships.
This package can generate a tailor-made SQLAlchemy Mixin that can be added to your SQLAlchemy Models, making them contain a field that, when set, will mark the entity as being soft-deleted.
The library also installs a hook which dynamically rewrites all selects which are sent to the database for all tables that implement the soft-delete mixin, providing a seamless experience in both manual queries and model relationship accesses.
Mixin generation is fully customizable and you can choose the field name, its type, and the presence of (soft-)delete/undelete methods.
The default implementation will generate a deleted_at field in your models, of type DateTime(timezone=True), and will also provide a .delete(v: Optional = datetime.utcnow()) and .undelete() methods.
Installation:
pip install sqlalchemy-easy-softdelete
How to use:
from sqlalchemy_easy_softdelete.mixin import generate_soft_delete_mixin_class
from sqlalchemy_easy_softdelete.hook import IgnoredTable
from sqlalchemy.orm import declarative_base, Mapped
from sqlalchemy import Column, Integer
from datetime import datetime
# Create a Class that inherits from our class builder
class SoftDeleteMixin(
generate_soft_delete_mixin_class( # type: ignore[misc]
# This table will be ignored by the hook
# even if the table has the soft-delete column
ignored_tables=[IgnoredTable(table_schema="public", name="cars"),]
)
):
# type: ignore[misc] is required because the mixin is dynamically generated
# Type hint for IDE autocomplete and type checker support.
# Using Mapped[T | None] ensures type checkers understand this is a
# SQLAlchemy column that supports query operations like .where()
deleted_at: Mapped[datetime | None]
# Optional: Add method stubs for delete/undelete for type checker support.
# The actual implementations are provided by the generated mixin class.
def delete(self, v: datetime | None = None) -> None:
super().delete(v) # type: ignore[misc]
def undelete(self) -> None:
super().undelete() # type: ignore[misc]
# Apply the mixin to your Models
Base = declarative_base()
class Fruit(Base, SoftDeleteMixin):
__tablename__ = "fruit"
id = Column(Integer)
Example Usage:
all_active_fruits = session.query(Fruit).all()
This will generate a query with an automatic WHERE fruit.deleted_at IS NULL condition added to it.
all_fruits = session.query(Fruit).execution_options(include_deleted=True).all()
Setting include_deleted=True (attribute name can be customized) in the query disables soft delete for that query.
Contributing
Contributions are welcome! See CONTRIBUTING.md for development setup and guidelines.
License
- BSD-3-Clause
Metadata
Release files for sqlalchemy-easy-softdelete 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlalchemy_easy_softdelete-0.9.1.tar.gz | 24.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlalchemy_easy_softdelete-0.9.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.1 kB
Release files / sqlalchemy_easy_softdelete-0.9.1.tar.gz
| Download URL | sqlalchemy_easy_softdelete-0.9.1.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02ce5b8eedf9c6207a041391ffa3906ea61b930559498c6e475781ce7dace599
|
|
BLAKE2b-256 checksum How to use checksums |
9b346dcceca68e6cbac616ea2fa4990103a224cfb2bb329a8694465eb7de48d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 16, 2026.
Transparency logRelease files / sqlalchemy_easy_softdelete-0.9.1-py3-none-any.whl
| Download URL | sqlalchemy_easy_softdelete-0.9.1-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7672778c2ce9969296219f3e973c1f0d17736ea04f53e05fa8e112c88adae2ed
|
|
BLAKE2b-256 checksum How to use checksums |
6eadc812c15adc5119c7c44e987e6997ded9530fd09bd1157d9862f70c40567d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 16, 2026.
Transparency log