How to Drop Tablespace Safely in Oracle (Complete Practical Guide)
Managing storage in Oracle Database often involves cleaning up unused tablespaces. However, dropping a tablespace is a critical and irreversible operation if not handled properly.
This guide explains:
DROP TABLESPACEsyntaxDifference between
INCLUDING CONTENTSandKEEP DATAFILESRisks and safety precautions
Real-world practical steps
๐ฆ 1. What is a Tablespace?
A tablespace is a logical storage unit in Oracle that contains datafiles. It stores:
Tables
Indexes
LOBs
Other database objects
Dropping a tablespace removes all objects stored inside it.
⚠️ 2. Basic DROP TABLESPACE Syntax
DROP TABLESPACE tablespace_name;
๐ This works only if the tablespace is empty
๐ฅ 3. Drop Tablespace with Contents
✅ Syntax
DROP TABLESPACE tablespace_name INCLUDING CONTENTS;
๐ What it does:
Deletes all objects inside the tablespace
Removes metadata from data dictionary
Does NOT delete physical datafiles
๐ Use this when you want to remove logical structures but keep files on disk.
๐ฃ 4. Drop Tablespace Including Datafiles
✅ Syntax
DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;
๐ What it does:
Deletes all objects
Deletes datafiles from OS
Completely frees disk space
⚠️ This is irreversible
๐ง 5. Keep Datafiles Option
✅ Syntax
DROP TABLESPACE tablespace_name INCLUDING CONTENTS KEEP DATAFILES;
๐ What it does:
Removes tablespace from database
Leaves datafiles untouched on disk
๐ Useful when:
You want to reuse files
You need backup/forensic analysis
⚖️ Summary of Options
| Command | Objects Removed | Datafiles Removed |
|---|---|---|
| DROP TABLESPACE | ❌ | ❌ |
| INCLUDING CONTENTS | ✅ | ❌ |
| INCLUDING CONTENTS AND DATAFILES | ✅ | ✅ |
| KEEP DATAFILES | ✅ | ❌ |
๐จ 6. Risks of Dropping Tablespace
Dropping a tablespace can cause serious issues if not planned properly:
❌ Data Loss
All tables and indexes inside will be permanently deleted.
❌ Application Failure
If application depends on that tablespace → system may break.
❌ Foreign Key Dependencies
Objects in other tablespaces may reference it.
❌ Backup Impact
If not backed up, recovery becomes impossible.
๐ก️ 7. Safety Checklist Before Dropping
✅ 1. Check Tablespace Contents
SELECT owner, segment_name, segment_type
FROM dba_segments
WHERE tablespace_name = 'USERS';
✅ 2. Check Datafiles
SELECT file_name
FROM dba_data_files
WHERE tablespace_name = 'USERS';
✅ 3. Check Default Tablespace Users
SELECT username
FROM dba_users
WHERE default_tablespace = 'USERS';
๐ Change users before dropping:
ALTER USER username DEFAULT TABLESPACE new_tablespace;
✅ 4. Take Backup (VERY IMPORTANT)
Using RMAN:
BACKUP TABLESPACE users;
✅ 5. Ensure No Active Usage
SELECT * FROM v$sort_usage WHERE tablespace = 'USERS';
๐งช 8. Practical Example (Step-by-Step)
๐ฏ Scenario: Drop tablespace OLD_DATA
Step 1: Verify contents
SELECT COUNT(*) FROM dba_segments
WHERE tablespace_name = 'OLD_DATA';
Step 2: Backup
BACKUP TABLESPACE old_data;
Step 3: Drop safely
DROP TABLESPACE old_data INCLUDING CONTENTS AND DATAFILES;
Step 4: Verify removal
SELECT * FROM dba_tablespaces
WHERE tablespace_name = 'OLD_DATA';
๐ง 9. Best Practices
✅ Always backup before dropping
✅ Never drop SYSTEM or SYSAUX tablespaces
✅ Avoid dropping USERS tablespace in production
✅ Check dependencies carefully
✅ Use KEEP DATAFILES if unsure
✅ Perform during maintenance window
๐ 10. Common Mistakes to Avoid
❌ Dropping without checking users
❌ Forgetting backup
❌ Confusing KEEP DATAFILES vs AND DATAFILES
❌ Dropping active tablespace
❌ Ignoring application impact
๐ Conclusion
Dropping a tablespace in Oracle Database is a powerful but risky operation. Understanding the difference between:
INCLUDING CONTENTSAND DATAFILESKEEP DATAFILES
is essential for safe database administration.
Always follow a structured approach: Check → Backup → Drop → Verify to avoid irreversible data loss.






0 comments:
Post a Comment