Introduction – What Is Soft Delete?
Soft delete is a data management technique that consists of marking records as deleted instead of physically removing them from the database. This approach allows deleted data to be recovered later if needed and preserves an audit history.
In practice, the user receives a message saying that their data has been deleted, but in reality, the data is still stored in the database.
If you are a Windows, Linux, or macOS user, you are already familiar with this concept through the trash bin. When you delete a file or folder, it is not immediately removed from your hard drive. Instead, it goes to the trash, where you can either permanently delete it or restore it to its original location.
Soft delete works in the same way: you delete an item or record through your application, it disappears from the interface, but it still exists in the database. Later, you can either permanently remove it or restore it as if nothing had happened.
Legal Implications
Soft delete can have important legal implications, especially in regulated industries. Some regulations, such as the General Data Protection Regulation (GDPR), may require keeping records of data changes for auditing purposes.
Soft delete can help meet these requirements by preserving an audit trail of deletions. However, if the user has not explicitly consented to this behavior, retaining their data—even in a “deleted” state—may raise legal concerns. It’s essential to align your implementation with privacy laws and user consent policies.
Implementation in Laravel
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class YourModel extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Laravel’s Eloquent ORM supports soft delete out of the box, without requiring any additional packages. With minimal configuration, you can delete, restore, or permanently remove records directly through your application.
Implementation in JavaScript
In the Node.js ecosystem, soft delete can be implemented in different ways depending on the database you are using. Below is an example using
Mongoose (commonly used with MongoDB):
const mongoose = require('mongoose');
const seuSchema = new mongoose.Schema({
// Seus campos de modelo
deleted: { type: Boolean, default: false }
}, { timestamps: true });
seuSchema.statics.softDelete = async function (id) {
return this.findByIdAndUpdate(id, { deleted: true });
};
seuSchema.statics.restore = async function (id) {
return this.findByIdAndUpdate(id, { deleted: false });
};
const SeuModelo = mongoose.model('SeuModelo', seuSchema);
This example introduces a deleted field that is set to true when a record is “deleted.” The model includes static methods (softDelete and restore) to simplify marking records as deleted and restoring them when needed.
Conclusion
Soft delete is a powerful and elegant feature that can greatly enhance any application. The examples shown here are just basic implementations, but the possibilities go much further.
For example:
You can build a trash system for blog posts.
In applications with user accounts, you can place accounts in a “trashed” state for a defined period after a deletion request.
After that period expires, the data can be permanently removed.
Used correctly, soft delete improves data safety, user experience, and system flexibility—while still respecting legal and ethical constraints.
Top comments (0)