Sobes.tech
Junior — Middle

Can you explain the concept of soft data deletion and how it differs from complete deletion?

sobes.tech AI

Answer from AI

Soft delete is an approach where data is not physically removed from the database but marked as deleted, for example, using a special field (such as is_deleted or deleted_at). This allows for data to be retained for possible recovery or auditing.

Hard delete is the physical removal of a record from the database, after which the data cannot be recovered.

An example of soft delete in PHP using the is_deleted field:

// Mark a record as deleted
function softDelete($id, $pdo) {
    $stmt = $pdo->prepare("UPDATE users SET is_deleted = 1 WHERE id = :id");
    $stmt->execute(['id' => $id]);
}

// Retrieve only non-deleted records
function getActiveUsers($pdo) {
    $stmt = $pdo->query("SELECT * FROM users WHERE is_deleted = 0");
    return $stmt->fetchAll();
}

Thus, soft delete helps avoid data loss and provides flexibility in data management.