DBA_RECYCLEBIN purge指定日期前的表

Summary

How to purge DBA_RECYCLBIN for objects older than x days/minutes? or do we have RECYCLEBIN RETENTION feature or truncate recyclebin ?


DBA_RECYCLEBIN has a column called DROPTIME.

But we can not use the below queries to purge or delete the recyclebin objects older than 7 days

DELETE from DBA_RECYCLEBIN where droptime<sysdate-7; (wrong do not use)

PURGE DBA_RECYCLEBIN where droptime<sysdate-7; (wrong do not use)

SQL> DELETE from DBA_RECYCLEBIN where droptime<sysdate-7;

DELETE from DBA_RECYCLEBIN where droptime<sysdate-7

*

ERROR at line 1:

ORA-01752: cannot delete from view without exactly one key-preserved table

SQL> PURGE DBA_RECYCLEBIN where droptime<sysdate-7;

PURGE DBA_RECYCLEBIN where droptime<sysdate-7

*

ERROR at line 1:

ORA-00933: SQL command not properly ended

Solution

There is no direct command or RECYCLEBIN_RETENTION parameter to purge/delete with the droptime clause, so we have to use the workaround of identifying the objects and then drop them manually. There is an enhancement request filed through an Unpublished Bug 6694377, so the feature is still not available, but there is an easy workaround available.

Unpublished Bug 6694377 : ENHANCE PURGE COMMAND WITH TIME SPECIFIC CLAUSE

SELECT .... FROM DBA_RECYCLEBIN can have WHERE condition,

PURGE DBA_RECYCLEBIN can not have WHERE condition and throws syntax errors like ORA-00933: SQL command not properly ended

So you can workaround this by dynamically preparing sql queries to purge the recycle bin objects

Examples:

  1. Purge tables dropped before 7 days (older than 7 days)

SQL> spool purge_table_older_than_7_days.sql

SQL> select 'purge table '||owner||'."'||OBJECT_NAME||'";'

from dba_recyclebin

where type='TABLE' and to_date(droptime,'YYYY-MM-DD:HH24:MI:SS')<sysdate-7;

SQL> spool off;

  1. Purge tables dropped before 5 minutes (older than 5 minutes), use sysdate-(5/(24*60)) because 24 hours per day, 60 minutes per hour

SQL> spool purge_table_older_than_5_minutes.sql

SQL> select 'purge table '||owner||'."'||OBJECT_NAME||'";'

from dba_recyclebin

where type='TABLE' and to_date(droptime,'YYYY-MM-DD:HH24:MI:SS')<sysdate-(5/(24*60));

SQL> spool off;

SQL> spool purge_table_older_than_5_min.txt

SQL> @purge_table_older_than_5_minutes.sql

SQL> spool off;

相关推荐
Lucifer三思而后行10 小时前
Oracle SQL 优化实战:明明走了索引,SQL 还是慢?
sql·oracle
DarkAthena17 小时前
【DuckDB】在DuckDB中查询ORACLE的数据的三种方式
oracle·duckdb
风123456789~18 小时前
【Oracle专栏】更新多个表的统计信息
oracle
实心儿儿21 小时前
MySQL — 库的操作
数据库·mysql·oracle
隔窗听雨眠1 天前
Oracle普通表自动改造分区表:从评估到自动化维护的完整实战指南
oracle
我要见SA姐11 天前
告别 Copilot?Codex 本地化部署指南
运维·数据库·机器学习·oracle·回归
我要见SA姐11 天前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
数据库·ide·vscode·oracle·编辑器
这个DBA有点耶1 天前
异构数据集成怎么做?5 种同步方案对比 + 金融级 CDC 实战解析
数据库·oracle·架构
oradh2 天前
Oracle联机日志文件损坏故障总结
数据库·oracle·oracle联机日志文件损坏
辰辉创聚2 天前
炎症与免疫相关细胞因子:信号通路、分类及科研检测应用
python·oracle·nycodenz·重组il-6蛋白·抗tnf-α抗体·il-1β蛋白