SQLite Reset Primary Key Field

121,561

Solution 1

Try this:

delete from your_table;    
delete from sqlite_sequence where name='your_table';

SQLite Autoincrement

SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes.

Solution 2

You can reset by update sequence after deleted rows in your-table

UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name';

Solution 3

As an alternate option, if you have the Sqlite Database Browser and are more inclined to a GUI solution, you can edit the sqlite_sequence table where field name is the name of your table. Double click the cell for the field seq and change the value to 0 in the dialogue box that pops up.

Share:
121,561
Author by

Nathan

Updated on July 08, 2022

Comments

  • Nathan 6 months

    I have a few tables in SQLite and I am trying to figure out how to reset the auto-incremented database field.

    I read that DELETE FROM tablename should delete everything and reset the auto-incremement field back to 0, but when I do this it just deletes the data. When a new record is inserted the autoincrement picks up where it left off before the delete.

    My ident field properties are as follows:

    • Field Type: integer
    • Field Flags: PRIMARY KEY, AUTOINCREMENT, UNIQUE

    Does it matter I built the table in SQLite Maestro and I am executing the DELETE statement in SQLite Maestro as well?

    Any help would be great.

  • 321X
    321X over 11 years
    Just a small side note: Table name in where clause is case sensitive
  • binary almost 11 years
    another way is updating sqlite_sequence table. update sqlite_sequence set seq = 0 where name='<tablename>' This will not reset the rowid of sqlite_sequence table.
  • Nick Dandoulakis
    Nick Dandoulakis almost 11 years
    @binary, thanks for mentioning an alternative way :) I'd like to note that SQLite reuses "deleted" space, so it doesn't really make much of a difference which solution we'll choose.
  • Chris Fong over 8 years
    Note that SQLite only creates the sqlite_sequence table when you define an autoincrement column. It doesn't exist before then.
  • Nick Dandoulakis
    Nick Dandoulakis over 8 years
    @ZacharyYates, indeed, but it's already mentioned on the quoted text.
  • dialex
    dialex about 7 years
    no such table sqlite_sequence is the error I get when I tried to run that command. What now?
  • Nick Dandoulakis
    Nick Dandoulakis about 7 years
    @dialex, Zachary Yates commented about that and it's also mentioned in my answer.
  • Farhan Ibn Wahid
    Farhan Ibn Wahid over 3 years
    can anyone tell how to do this for room ?
  • Farhan Ibn Wahid
    Farhan Ibn Wahid over 3 years
    can you tell me how to do this for room ?
  • Androidcoder
    Androidcoder over 3 years
    I'm getting: 'SQLiteException: no such column' error on second line of code with this.
  • Huỳnh Ngọc Bang
    Huỳnh Ngọc Bang about 3 years
    what do you mean @Psycho ? I can't understand :)
  • Farhan Ibn Wahid
    Farhan Ibn Wahid about 3 years
    actually I was asking how to do this in Room Database..... well the problem is solved for now
  • prakashpun
    prakashpun almost 3 years
    @Psycho can you tell me how you used this using Room database?
  • GSandro_Strongs
    GSandro_Strongs about 2 years
    Great, fir me it worked, however I have a question. Why the size of the file is just the same despote we deleted the rows?
  • Nick Dandoulakis
    Nick Dandoulakis about 2 years
    @GSandro_Strongs, the size is the same because SQLite reuse the "deleted" space. There's the VACUUM command that rebuilds the database file to take the minimal amount of disk space.
  • Ojonugwa Jude Ochalifu
    Ojonugwa Jude Ochalifu about 1 year
    How does this help for an app in production?
  • Clive Long
    Clive Long 10 months
    This answer is very clear, easy to implement and easy to verify that it works