๐Ÿฅž BE
home

Soft Delete

Date
2024/11/22
Category
DB
Tag
Basic
Detail

Soft Delete vs Hard Delete

Soft Delete (๋…ผ๋ฆฌ ์‚ญ์ œ)

DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ , ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๋Š” ๋ฐฉ์‹.
๋ณดํ†ต์€ is_deleted ์ปฌ๋Ÿผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด boolean ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์šฉ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. is_deleted = 0 ์ด๋ฉด ์กฐํšŒ ๊ฐ€๋Šฅ, is_deleted = 1 ์ด๋ฉด ์กฐํšŒ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ฒ˜๋Ÿผ. ๋•Œ๋ฌธ์—, ํ•ด๋‹น ํ•„๋“œ์™€ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ ์‹œ, ์ด์— ๋Œ€ํ•ด ์ฒดํฌํ•˜๋Š” ์ฟผ๋ฆฌ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค.
update user set is_delete = 1 where user_name = "hello"
SQL
๋ณต์‚ฌ

Hard Delete (๋ฌผ๋ฆฌ ์‚ญ์ œ)

DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ์‹. ๋”์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ DB์—์„œ ์™„์ „ํžˆ ์‚ญ์ œํ•ด๋ฒ„๋ฆฌ๋ฉฐ, ์ €์žฅ ๊ณต๊ฐ„์„ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
ํ•ด๋‹น ๋ฐ์ดํ„ฐ ํ–‰์— ๋Œ€ํ•ด ๊ทธ๋ƒฅ delete๋ฅผ ์จ๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

Soft Delete๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์ด์œ 

1.
์‚ฌ์šฉ์ž, ๊ฒ€์ƒ‰ ๊ธฐ๋ก๊ณผ ๊ฐ™์ด ๋” ๋‚˜์€ ์„œ๋น„์Šค ๊ฐœ์„ ์„ ์œ„ํ•ด ๋ถ„์„ํ•ด์•ผํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์ด ํ•„์š”
2.
๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๋ณต๊ตฌํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ
3.
update ์ฟผ๋ฆฌ๊ฐ€ delete ์ฟผ๋ฆฌ๋ณด๋‹ค ์ชผ์˜ค๊ธˆ ๋” ๋น ๋ฆ„

Reference

[JPA] Soft Delete: JPA์—์„œ Soft Delete๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•, @SqlDelete, @Where
ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋˜ ๋„์ค‘, ์šฐ๋ฆฌ ํŒ€์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ soft deleteํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ๋จผ์ € Soft Delete, Hard Delete์˜ ์ฐจ์ด์ ๋ถ€ํ„ฐ ๊ฐ„๋‹จํžˆ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋ณด์ž! ๐Ÿ’‹ Soft Delete VS Hard Delete โœ” Soft Delete ๋…ผ๋ฆฌ์  ์‚ญ์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์ œ๋กœ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ , ์‚ญ์ œ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•˜์—ฌ ์‚ญ์ œ๋œ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ฐ์ดํ„ฐ๋Š” ์‹œ์Šคํ…œ์—์„œ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ, ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ณต๊ฐ„ ์ ˆ์•ฝ๊ณผ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์˜ ํŽธ์˜์„ฑ์„ ์œ„ํ•ด ์œ ์šฉํ•˜์ง€๋งŒ, ๋ณต๊ตฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์‹ ์ค‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ๋ณด์กด์„ ์œ„ํ•ด ์œ ์šฉํ•˜๋ฉฐ, ์‹ค์ˆ˜๋กœ ์‚ญ์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ์‚ญ์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ์— ์‚ญ์ œ ์—ฌ๋ถ€๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•  ์ˆ˜..