<p>Working on a project at the moment and we have to implement soft deletion for the majority of users (user roles). We decided to add an <code>is_deleted='0'</code> field on each table in the database and set it to <code>'1'</code> if particular user roles hit a delete button on a specific record.</p>

<p>For future maintenance now, each <code>SELECT</code> query will need to ensure they do not include records <code>where is_deleted='1'</code>.</p>

<p>Is there a better solution for implementing soft deletion?</p>

<p>Update: I should also note that we have an Audit database that tracks changes (field, old value, new value, time, user, ip) to all tables/fields within the Application database.</p>

[{'answer_id': 68328, 'author': 'David J. Sokol', 'author_id': 1390, 'author_profile': 'https://Stackoverflow.com/users/1390', 'pm_score': 7, 'selected': True, 'text': "<p>You could perform all of your queries against a view that contains the <code>WHERE IS_DELETED='0'</code> clause.</p>\n"}, {'answer_id': 68333, 'author': 'Jiaaro', 'author_id': 2908, 'author_profile': 'https://Stackoverflow.com/users/2908', 'pm_score': 4, 'selected': False, 'text': "<p>if the table is large and performance is an issue, you can always move 'deleted' records to another table, which has additional info like time of deletion, who deleted the record, etc</p>\n\n<p>that way you don't have to add another column to your primary table</p>\n"}, {'answer_id': 68338, 'author': 'ctcherry', 'author_id': 10322, 'author_profile': 'https://Stackoverflow.com/users/10322', 'pm_score': 7, 'selected': False, 'text': '<p>I would lean towards a <code>deleted_at</code> column that contains the <strong>datetime of when the deletion took place</strong>. Then you get a little bit of free metadata about the deletion. For your <code>SELECT</code> just get rows <code>WHERE deleted_at IS NULL</code></p>\n'}, {'answer_id': 68378, 'author': 'Daniel Fortunov', 'author_id': 5975, 'author_profile': 'https://Stackoverflow.com/users/5975', 'pm_score': 4, 'selected': False, 'text': '<p>That depends on what information you need and what workflows you want to support.</p>\n\n<p>Do you want to be able to:</p>\n\n<ul>\n<li>know what information was there (before it was deleted)?</li>\n<li>know when it was deleted?</li>\n<li>know who deleted it?</li>\n<li>know in what capacity they were acting when they deleted it?</li>\n<li>be able to un-delete the record?</li>\n<li>be able to tell when it was un-deleted?</li>\n<li>etc.</li>\n</ul>\n\n<p>If the record was deleted and un-deleted four times, is it sufficient for you to know that it is currently in an un-deleted state, or do you want to be able to tell what happened in the interim (including any edits between successive deletions!)? </p>\n'}, {'answer_id': 68380, 'author': 'Brent', 'author_id': 10680, 'author_profile': 'https://Stackoverflow.com/users/10680', 'pm_score': 3, 'selected': False, 'text': '<p>You will definitely have better performance if you move your deleted data to another table like Jim said, as well as having record of when it was deleted, why, and by whom.</p>\n\n<p>Adding <code>where <code>deleted</code>=0</code> to all your queries will slow them down significantly, and hinder the usage of any of indexes you may have on the table. Avoid having "flags" in your tables whenever possible.</p>\n'}, {'answer_id': 68396, 'author': 'Sergey Stadnik', 'author_id': 10557, 'author_profile': 'https://Stackoverflow.com/users/10557', 'pm_score': 5, 'selected': False, 'text': "<p>Having <code>is_deleted</code> column is a reasonably good approach.\nIf it is in Oracle, to further increase performance I'd recommend partitioning the table by creating a list partition on <code>is_deleted</code> column.\nThen deleted and non-deleted rows will physically be in different partitions, though for you it'll be transparent.</p>\n\n<p>As a result, if you type a query like</p>\n\n<pre><code>SELECT * FROM table_name WHERE is_deleted = 1\n</code></pre>\n\n<p>then Oracle will perform the 'partition pruning' and only look into the appropriate partition. Internally a partition is a different table, but it is transparent for you as a user: you'll be able to select across the entire table no matter if it is partitioned or not. But Oracle will be able to query <strong>ONLY the partition it needs</strong>. For example, let's assume you have 1000 rows with <code>is_deleted = 0</code> and 100000 rows with <code>is_deleted = 1</code>, and you partition the table on <code>is_deleted</code>. Now if you include condition</p>\n\n<pre><code>WHERE ... AND IS_DELETED=0\n</code></pre>\n\n<p>then Oracle will ONLY scan the partition with 1000 rows. If the table weren't partitioned, it would have to scan 101000 rows (both partitions).</p>\n"}, {'answer_id': 68438, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 0, 'selected': False, 'text': '<p>I prefer to keep a status column, so I can use it for several different configs, i.e. published, private, deleted, needsAproval...</p>\n'}, {'answer_id': 69160, 'author': 'Steven A. Lowe', 'author_id': 9345, 'author_profile': 'https://Stackoverflow.com/users/9345', 'pm_score': 0, 'selected': False, 'text': "<p>Use a view, function, or procedure that checks <code>is_deleted = 0</code>; i.e. don't select directly on the table in case the table needs to change later for other reasons.</p>\n\n<p>And index the <code>is_deleted</code> column for larger tables.</p>\n\n<p>Since you already have an audit trail, tracking the deletion date is redundant.</p>\n"}, {'answer_id': 69605, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Something that I use on projects is a statusInd tinyint not null default 0 column\nusing statusInd as a bitmask allows me to perform data management (delete, archive, replicate, restore, etc.). Using this in views I can then do the data distribution, publishing, etc for the consuming applications. If performance is a concern regarding views, use small fact tables to support this information, dropping the fact, drops the relation and allows for scalled deletes.</p>\n\n<p>Scales well and is data centric keeping the data footprint pretty small - key for 350gb+ dbs with realtime concerns.  Using alternatives, tables, triggers has some overhead that depending on the need may or may not work for you.  </p>\n\n<p>SOX related Audits may require more than a field to help in your case, but this may help.\nEnjoy</p>\n'}, {'answer_id': 71825, 'author': 'Jeremiah Peschka', 'author_id': 11780, 'author_profile': 'https://Stackoverflow.com/users/11780', 'pm_score': 4, 'selected': False, 'text': "<p>The best response, sadly, depends on what you're trying to accomplish with your soft deletions and the database you are implementing this within.</p>\n\n<p>In SQL Server, the best solution would be to use a deleted_on/deleted_at column with a type of SMALLDATETIME or DATETIME (depending on the necessary granularity) and to make that column nullable.  In SQL Server, the row header data contains a NULL bitmask for each of the columns in the table so it's marginally faster to perform an IS NULL or IS NOT NULL than it is to check the value stored in a column.</p>\n\n<p>If you have a large volume of data, you will want to look into partitioning your data, either through the database itself or through two separate tables (e.g. Products and ProductHistory) or through an indexed view.</p>\n\n<p>I typically avoid flag fields like is_deleted, is_archive, etc because they only carry one piece of meaning.  A nullable deleted_at, archived_at field  provides an additional level of meaning to yourself and to whoever inherits your application.  And I avoid bitmask fields like the plague since they require an understanding of how the bitmask was built in order to grasp any meaning.</p>\n"}, {'answer_id': 72749, 'author': 'Andy Irving', 'author_id': 8553, 'author_profile': 'https://Stackoverflow.com/users/8553', 'pm_score': 2, 'selected': False, 'text': "<p>you don't mention what product, but SQL Server 2008 and postgresql (and others i'm sure) allow you to create filtered indexes, so you could create a covering index where is_deleted=0, mitigating some of the negatives of this particular approach.</p>\n"}, {'answer_id': 5884806, 'author': 'Andy Rappaport', 'author_id': 738183, 'author_profile': 'https://Stackoverflow.com/users/738183', 'pm_score': 3, 'selected': False, 'text': '<p>Careful of soft-deleted records causing uniqueness constraint violations.\nIf your DB has columns with unique constraints then be careful that the prior soft-deleted records don’t prevent you from recreating the record. </p>\n\n<p>Think of the cycle:  </p>\n\n<ol>\n<li>create user (login=JOE)</li>\n<li>soft-delete  (set deleted column to non-null.)</li>\n<li>(re) create user (login=JOE).  ERROR.  LOGIN=JOE is already taken</li>\n</ol>\n\n<p>Second create results in a constraint violation because login=JOE is already in the soft-deleted row.  </p>\n\n<p>Some techniques:\n 1. Move the deleted record to a new table.\n 2. Make your uniqueness constraint across the login and deleted_at  timestamp column</p>\n\n<p>My own opinion is +1 for moving to new table.   Its take lots of\n    discipline to maintain the *AND delete_at = NULL* across all your\n    queries (for all of your developers)</p>\n'}, {'answer_id': 6491996, 'author': 'adsm', 'author_id': 817229, 'author_profile': 'https://Stackoverflow.com/users/817229', 'pm_score': 0, 'selected': False, 'text': '<p>Create an other schema and grant it all on your data schema.\nImplment VPD on your new schema so that each and every query will have the predicate allowing selection of the non-deleted row only appended to it.\n<a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/cmntopc.htm#CNCPT62345" rel="nofollow">http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/cmntopc.htm#CNCPT62345</a></p>\n'}, {'answer_id': 23786134, 'author': 'Kalpesh Soni', 'author_id': 255139, 'author_profile': 'https://Stackoverflow.com/users/255139', 'pm_score': -1, 'selected': False, 'text': '<pre><code>@AdditionalCriteria("this.status &lt;&gt; \'deleted\'")\n</code></pre>\n\n<p>put this on top of your <code>@entity</code></p>\n\n<p><a href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/SoftDelete" rel="nofollow noreferrer">http://wiki.eclipse.org/EclipseLink/Examples/JPA/SoftDelete</a></p>\n'}]
['https://Stackoverflow.com/questions/68323', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10583/']