Delete old tasks, events, statistics data and sequences reset in vCenter Server 5.x, 6.x and 7.x
search cancel

Delete old tasks, events, statistics data and sequences reset in vCenter Server 5.x, 6.x and 7.x

book

Article ID: 326245

calendar_today

Updated On:

Products

VMware vCenter Server

Issue/Introduction

Managing the Tasks, Events and Statistics stored in vCenter Server database is a common responsibility of the VMware Administrator. In this KB we will use scripts to help with this operation and keep vCenter Server's database healthy.

Regular maintenance of Tasks, Events and Statistics helps in avoiding performance and instability problems in VCenter Server and other components that depend on VCenter's operation.

Environment

VMware vCenter Server 7.0.x
VMware vCenter Server 6.x

Resolution

Reducing the historical data of tasks, events and statistics records by using a script for your type of database.

Important: You can download the scripts for supported database types in the Attachments section on the right panel of this article. You may use WinSCP or similar tools to transfer file in case of vCenter Server Appliance. (To enable WinSCP file transfer refer: Error when uploading files to vCenter Server Appliance using WinSCP )

These scripts work for vCenter 5.x, 6.x and 7.x databases.

Process to reduce the historical data:

To reduce the data, perform these steps:

  1. Stop the vCenter Server service
  2. Back up the vCenter Server database
  3. Run the script for your database type
  4. Reset the event id sequence (only if we are deleting all events)
 

Stop the vCenter Server service (vpxd)

  1. If you installed vCenter Server on a Windows machine:
For more information, see:
 
  1. If you deployed the vCenter Server Appliance:
For more information, see:
 

Understanding the scripts’ parameters

The scripts contain three main parameters:

•    TaskMaxAgeInDays

o    Deletes tasks older than the specified number of days

•    EventMaxAgeInDays

o    Deletes events older than the specified number of days

•    StatMaxAgeInDays

o    Deletes statistics older than the specified number of days


The possible values for all the parameters are:
-1 = Does not delete data. Ex. TaskMaxAgeInDays = -1, means that no task records will be deleted.
0 = Deletes all data. Ex. TaskMaxAgeInDays = 0, deletes all task records. If all data are deleted it is recommended that the sequences should be reset.
>1 = Deletes data older than the number you entered. Ex. TaskMaxAgeInDays = 10, leaves the task records gathered within the last 10 days and deletes all the records gathered before that.
 

Run the script for your database

 

vPostgres for Windows
a. Log in to the machine in which vCenter Server is running.
b. Download the  2110031_Postgres_task_event_stat script attached to this KB article.
Note: For vCenter 6.5 and later download 2110031_Postgres_task_event_stat_new.sql
c. Run the script by using psql and when prompted provide the vCenter Server user password.
 

C:\Program Files\VMware\vCenter Server\vPostgres\bin\psql -U <vCenter Server database user> -v TaskMaxAgeInDays=<days to keep> -v EventMaxAgeInDays=<days to keep> -v StatMaxAgeInDays=<days to keep> -d database-name <DATABASE> -t -q -f download-path\2110031_PostgreSQL_task_event_stat.sql


For 6.5 and later:

C:\Program Files\VMware\vCenter Server\vPostgres\bin\psql -U <vCenter Server database user> -v TaskMaxAgeInDays=<days to keep> -v EventMaxAgeInDays=<days to keep> -v StatMaxAgeInDays=<days to keep> -d database-name <DATABASE></DATABASE>-t -q -f download-path\2110031_PostgreSQL_task_event_stat_new.sql


Note: To find the database password locate the vcdb.properties file in the %VMWARE_CFG_DIR%\vmware-vpx\ folder and open the file using a text editor. In the vcdb.properties file, locate the password of the vc database user and record it.


vPostgres for vCenter Appliance

  1. Run this command to execute the script:

/opt/vmware/vpostgres/current/bin/psql -U postgres -v TaskMaxAgeInDays=<days to keep> -v EventMaxAgeInDays=<days to keep> -v StatMaxAgeInDays=<days to keep> -d VCDB -t -q -f <download-path>/2110031_Postgres_task_event_stat.sql
 

For 6.5 and later:

/opt/vmware/vpostgres/current/bin/psql -U postgres -v TaskMaxAgeInDays=<days to keep> -v EventMaxAgeInDays=<days to keep> -v StatMaxAgeInDays=<days to keep> -d VCDB -t -q -f <download-path>/2110031_Postgres_task_event_stat_new.sql

 

Microsoft SQL Server

a.    Log in to the Microsoft SQL Server machine as an administrator.
b.    Download and save the 2110031_MS_SQL_task_event_stat script attached to this Knowledge Base article.
c.    Open the command prompt and run the script:

sqlcmd -S <IP address or FQDN of the database machine>\<instance_name> -U <vCenter Server database user> -P <password> -d <database name> -v TaskMaxAgeInDays=<days to keep> -v EventMaxAgeInDays=<days to keep> -v StatMaxAgeInDays=<days to keep> -i <download-path>\2110031_MS_SQL_task_event_stat.sql

 

Oracle

  1. Log in to the Oracle machine
  2. Download and save the 2110031_Oracle_task_event_stat script attached to this Knowledge Base article.
  3. Log in to sqlplus with the vCenter Server database username and password:

sqlplus <vCenter Server database user>/<password>

  1. Run the script:

@ <PATH></PATH>/2110031_Oracle_task_event_stat <days to keep tasks> <days to keep events> <days to keep stats>

Note: The order of the task, event and stat days is critical for the correct script execution.
 

Reset event id sequence

In case of deleting all the events it is recommended to reset the sequence that generates ids for the events as it will cause an issue if it at some point exceeds the maximum value of 32-bit integer. The maximum value is 2,147,483,647.
 

Checking sequence value and resetting it

PostgreSQL

Check sequence value
SELECT nextval('vc.vpx_event_seq');
Reset sequence value
SELECT setval('vc.vpx_event_seq', 1, false);
 

Microsoft SQL

Check sequence value
SELECT NEXT VALUE FOR VPX_EVENT_SEQ;
Reset sequence value
ALTER SEQUENCE VPX_EVENT_SEQ RESTART WITH 1;

 

Oracle

Check sequence value
SELECT vpx_event_seq.nextval FROM DUAL;
Reset sequence value
DROP SEQUENCE VPX_EVENT_SEQ;

CREATE SEQUENCE VPX_EVENT_SEQ
START WITH     1
INCREMENT BY   1000
NOCYCLE;

Additional Information

Virtual machines on an ESXi host appear orphaned in VMware vCenter Server 5.1 and 5.5.
Resolution: vCenter Server Tasks and Events Retention Period Changed.

Attachments

2110031_Postgres_task_event_stat_new.sql get_app
2110031_MS_SQL_task_event_stat.sql get_app
2110031_Oracle_task_event_stat.sql get_app
2110031_Postgres_task_event_stat.sql get_app