简体   繁体   中英

Soft delete and recovery (MySQL, PHP)

I need to be able to delete messages and to restore them until the page is not reloaded. So I have a field 'is_deleted' in my 'messages' table. What I want is to delete these 'deleted' records after it. What would be the best solution for this?

It depends on your needs, a "purge" button may be what you want or a lot of times people will use a cron job and set the is_deleted to be a Nullable DATETIME field, if after x days, it still shows as being deleted, it is automatically deleted from the database.

So if the automatic way appeals to you and you are on linux look at Cron jobs or if on windows Task Scheduler. If you would like an example of the cron, I need to know how frequently you would want the records purged from the system and I will provide you with an example of one.

For example, to run the delete script every 5 minutes:

*/5 * * * * /usr/bin/php -f /path/to/script/purgeDeleted.php > /dev/null

You will still have to write the purgeDeleted.php script, but that is the setup for running a php cli script via a cron job in Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM