TRUNCATE - potatoscript/sqlite GitHub Wiki

🍬 What is TRUNCATE?

Think of TRUNCATE as a super vacuum cleaner 🧹
It clears out a whole table in one big whoooosh πŸŒͺ️

It’s like saying:

β€œRemove ALL the toys from the toy box, FAST!” 🧸πŸͺ€πŸͺπŸ§Ό


❗ Important: SQLite Does NOT Support TRUNCATE

Wait what?! 😱
That’s right β€” SQLite does not use TRUNCATE like MySQL or PostgreSQL do.

But don’t worry β€” I’ll still explain it (because you might use MySQL or PostgreSQL someday).
And I’ll show you what to use instead in SQLite πŸ’‘


🧠 What does TRUNCATE do (in other DBs)?

TRUNCATE TABLE table_name;
  • Deletes ALL rows βœ…
  • But does not log each row (faster than DELETE) πŸš€
  • Auto-resets IDs (like AUTO_INCREMENT) to 1 πŸ”„

⚑ Faster than:

DELETE FROM table_name;

πŸ“ Example (in MySQL/PostgreSQL)

You have a table called fruits:

TRUNCATE TABLE fruits;

πŸ’₯ Boom! All your fruits are gone. πŸŽπŸŒπŸ‡
Table is empty, but still exists (unlike DROP).


πŸ§ͺ What about in SQLite?

🚫 SQLite doesn't support TRUNCATE.

Instead, you do:

DELETE FROM table_name;

βœ… Same result: all rows deleted
❌ IDs not reset by default


πŸ’ How to Reset the IDs in SQLite?

Let’s say you have a table with AUTOINCREMENT.

After deleting everything:

DELETE FROM my_table;

You then reset the sequence like this:

DELETE FROM sqlite_sequence WHERE name = 'my_table';

✨ That resets the auto-increment counter back to 1!


πŸ’‘ Summary Table

Feature DELETE TRUNCATE (Not in SQLite)
Deletes all rows βœ… βœ…
Logs each row βœ… ❌
Faster ❌ βœ…
Resets auto-increment ID ❌ (need extra step) βœ…
Supported in SQLite? βœ… ❌