---
title: "Deduplication & integrity — Media Library for Laravel | Roundly"
description: "Content-addressable storage with refcount-guarded deletes that stay safe under concurrent writes, plus an optional read-time checksum check."
url: https://roundly-consulting.com/open-source/docs/media-library-for-laravel/deduplication
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Media Library for Laravel](https://roundly-consulting.com/open-source/docs/media-library-for-laravel.md)

# Deduplication & integrity

With deduplicate enabled, identical bytes on the same (disk, visibility) are stored once and shared by reference, as long as the shared file is still on the disk — and attach() and a same-disk copy() share an original the same way. A reference count on the stored file keeps sharing safe: a physical original is removed — by a delete, a cross-disk move or a replace — only when no other row, soft-deleted rows included, still points at the same file (disk + path), so a soft delete followed by a restore stays lossless even while the file is shared:

```php
// Identical bytes on the same (disk, visibility) are stored once.
$a = MediaLibrary::for($userA)->add($path)->toBucket('docs');   // the source path is only read
$b = MediaLibrary::for($userB)->add($path)->toBucket('docs'); // same bytes -> shared storage

// Deleting $a keeps the file alive while $b still references it.
MediaLibrary::delete($a); // the original stays until the last reference is gone

$b->verifyIntegrity();    // bool — false on drift or a missing file
```

A content checksum (sha256 by default) backs dedup and optional read-time integrity verification — set verify\_checksum\_on\_read to re-hash the original on stream/download and throw ChecksumMismatch on drift. Each file is verified with the algorithm its checksum was taken with, so changing media.checksum\_algorithm never reports older media as drifted. Variants are never shared: each media generates its own. A still-shared original is never overwritten either — replacing one sharer writes its new bytes to a path of its own — and directories are only tidied away once nothing another row points at is left in them.

## Concurrent writes and rollbacks

A row that starts sharing a file is invisible to other connections until it commits, so the reference count alone could miss it. The package closes that gap with row locks and after-commit deletes:

- Every way a row starts sharing an original — a deduplicated add or replace, attach(), a same-disk copy() — saves it under a row lock (SELECT … FOR UPDATE, inside a transaction of its own — a savepoint in yours) on a row that already stores the file. A concurrent delete or move of that row waits for the new referrer to commit, then sees it. Locking one referrer is enough: while it still stores the file, no other row’s check removes it.
- Should the row being shared be gone — or hold other bytes — once the lock is held, a deduplicating add or replace stores its own copy instead, while attach() and copy() throw ModelNotFoundException.
- A replace locks its own row before it decides whether to overwrite its original in place, so it never overwrites bytes a new referrer is about to share.
- A force delete removes its files once the outermost transaction commits (at once when none is open): a rollback keeps them, and the reference count then sees a row another connection made share the original meanwhile. A move, a variant move, a draft bind and a replace release their old files the same way.
- On a real database engine two such flows can deadlock, each holding a row the other wants. The database aborts one with a deadlock error, which the caller can retry.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/media-library-for-laravel.md)
