PHPackages                             chrisbaltazar/doctrine-soft-delete - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. chrisbaltazar/doctrine-soft-delete

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

chrisbaltazar/doctrine-soft-delete
==================================

Symfony Bundle to handle Soft deleted records with Doctrine

1.2.1(1w ago)02↓100%MITPHPPHP &gt;=8.1

Since Apr 20Pushed 1w agoCompare

[ Source](https://github.com/chrisbaltazar/doctrine-soft-delete)[ Packagist](https://packagist.org/packages/chrisbaltazar/doctrine-soft-delete)[ RSS](/packages/chrisbaltazar-doctrine-soft-delete/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (5)Versions (7)Used By (0)

doctrine-soft-delete
====================

[](#doctrine-soft-delete)

A Symfony Bundle to handle **soft-deleted records** transparently with Doctrine ORM and MySQL.

Instead of permanently deleting rows, soft-delete marks records with a `deleted_at` timestamp and automatically excludes them from all queries via a Doctrine SQL filter.

[![PHP >= 8.1](https://camo.githubusercontent.com/1a5e13126d38c1d05f712dae30e7f60ae0444a9c882e9e526349ccba27facb8d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344382e312d626c7565)](https://www.php.net/)[![License: MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](LICENSE)

---

Features
--------

[](#features)

- ✅ Automatic `deleted_at` column filtering via a Doctrine SQL filter
- ✅ MySQL `GENERATED` column (`auto_generated_active_flag`) for safe unique indexes on soft-deletable tables
- ✅ PHP 8 attribute `#[SoftDeleteUniqueIndex]` to declare unique constraints that respect soft deletes
- ✅ Custom MySQL schema comparator that prevents unnecessary migration noise
- ✅ Zero-config autowiring via Symfony's service container

---

Compatibility
-------------

[](#compatibility)

- Symfony 6.4+
- Doctrine ORM 3.6+
- MySQL 5.7+ (for generated columns)

Dependencies
------------

[](#dependencies)

DependencyVersionPHP`>= 8.1``symfony/framework-bundle``^6.4``doctrine/doctrine-bundle``^2.18``doctrine/orm``^3.6``doctrine/doctrine-migrations-bundle``^3.7`---

Installation
------------

[](#installation)

```
composer require chrisbaltazar/doctrine-soft-delete
```

Please make sure the bundle gets registered in your `config/bundles.php`: (normally handled by Symfony Flex)

```
return [
    // ...
    Database\SoftDelete\SoftDeleteBundle::class => ['all' => true],
];
```

Usage
-----

[](#usage)

To auto enable the *soft delete* function, simply implement the `SoftDeletableInterface` in your entity and add a nullable `deletedAt` property:

```
use Database\SoftDelete\Core\Contract\SoftDeletableInterface;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Product implements SoftDeletableInterface
{
    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $deletedAt = null;

    public function setDeletedAt(\DateTimeImmutable $deletedAt): void
    {
        $this->deletedAt = $deletedAt;
    }

    public function getDeletedAt(): ?\DateTimeImmutable
    {
        return $this->deletedAt;
    }
}
```

Then just remember to update your entities accordingly when you want to soft-delete a record:

```
$product->setDeletedAt(new \DateTimeImmutable());
$entityManager->flush();
```

This will automatically exclude soft-deleted records from all queries.

### Temporarily include soft-deleted records

[](#temporarily-include-soft-deleted-records)

To include them, you can disable the filter at any point during your request flow with:

```
$entityManager->getFilters()->disable('soft_delete');
...
$users = $entityManager->getRepository(User::class)->findAll(); // includes soft-deleted records
```

### Unique soft-deletable records

[](#unique-soft-deletable-records)

For unique items that should ignore soft-deleted records, use the `#[SoftDeleteUniqueIndex]` attribute: This will create an auto-generated column in your entity table as a boolean flag + a table index, that would control the uniqueness behavior of the specified fields, ignoring soft-deleted records (where `deleted_at` is not null).

```
use Database\SoftDelete\Core\Attribute\SoftDeleteUniqueIndex;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[SoftDeleteUniqueIndex(fields: ['email'])]
class User implements SoftDeletableInterface
{
    // ...
}
```

After that just generate a new migration as normal and execute it to update your database schema.

Then simply handle the unique constraint violation properly as the following example:

```
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
...
// Perform form validation and any other necessary checks before attempting to persist the user entity
...

try {
    $entityManager->persist($user);
    $entityManager->flush();
} catch (UniqueConstraintViolationException) {
    $this->addFlash('error', 'A user with this email already exists.');
}

// Continue with the rest of your controller logic, such as rendering a response or redirecting the user
```

Demo
----

[](#demo)

A demo Symfony application is available in the `demo/` directory of this repository, showcasing the bundle's features in action. To run the demo locally just use the handy commands available in the `Makefile`:

(Make sure you have already a docker environment well configured with `docker-compose` and `Make` installed on your machine)

```
make demo-run
```

After that you should be able to access the demo app at `http://localhost:8000` and see how soft-deleted records are handled in the UI. (the port number can be also customized in the `docker-compose.override.yml` file if needed)

Then just go over: `http://localhost:8000/user` and try it out as any other CRUD 🚀

Testing
-------

[](#testing)

To run the tests, simply execute:

```
make test
```

This will run all PHPUnit tests defined in the `tests/` directory, ensuring that the bundle's functionality is working as expected, ensuring the special order and conditions needed to properly test the database custom components and the generated column behavior.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance98

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

4

Last Release

8d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8417691?v=4)[Christian Baltazar](/maintainers/chrisbaltazar)[@chrisbaltazar](https://github.com/chrisbaltazar)

---

Top Contributors

[![chrisbaltazar](https://avatars.githubusercontent.com/u/8417691?v=4)](https://github.com/chrisbaltazar "chrisbaltazar (13 commits)")

### Embed Badge

![Health badge](/badges/chrisbaltazar-doctrine-soft-delete/health.svg)

```
[![Health](https://phpackages.com/badges/chrisbaltazar-doctrine-soft-delete/health.svg)](https://phpackages.com/packages/chrisbaltazar-doctrine-soft-delete)
```

###  Alternatives

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.5M370](/packages/easycorp-easyadmin-bundle)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1155.2k](/packages/rcsofttech-audit-trail-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M195](/packages/sulu-sulu)[kimai/kimai

Kimai - Time Tracking

4.7k8.7k1](/packages/kimai-kimai)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1715.6k12](/packages/2lenet-crudit-bundle)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

9017.2k55](/packages/open-dxp-opendxp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
